代码之家  ›  专栏  ›  技术社区  ›  Alfabravo313

循环(for loop),即使是jython中图片中的像素

  •  1
  • Alfabravo313  · 技术社区  · 8 年前

    我需要使用单for循环遍历图片中的每个偶数像素。我想我已经接近这个代码了,但jython不喜欢它,我也不知道为什么(第二个for循环)。

    for x in range(0, width):
      for y in range(0, height):
        px = getPixels(pic, x, y)
    

    任何帮助都将不胜感激。

    如果有帮助的话,这是我的完整代码。该项目的重点是通过将所有偶数像素移动到大小为一半的新空白图片来调整图片大小。

    def main():
      #Allows the user to pick a picture
        pic = makePicture(pickAFile())
        show(pic)
        #Finds the width and height of the selected picture
        width = getWidth(pic)
        height = getHeight(pic)
    
      #Finds and divides width accordingly
        if width % 2 == 0:
          newW = width/2
        else:
          newW = width/2+1
    
      #Finds and divides height accordingly  
        if height % 2 == 0:
          newH = height/2
        else:
          newH = height/2+1
    
    for x in range(0, width, 2):
      for y in range(0, height, 2):
        px = getPixels(pic, x, y)
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   kylew    8 年前

    对于y值,是否存在不在循环上切换的问题?这样构建文本可以解决问题吗?(顺便说一句,在range()函数中添加第三个参数可以定义步长值,而不是默认的1。)

    for x in range(0, width, 2):
        for y in range(0, height, 2):
            px = getPixels(pic, x, y)