【问题标题】:loop (for loop) though even pixels in a picture in jython循环(for循环),即使是jython中图片中的像素
【发布时间】:2017-11-20 22:33:16
【问题描述】:

我需要使用单个 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)

【问题讨论】:

    标签: image loops for-loop jython pixels


    【解决方案1】:

    没有在循环中选择 y 值是否有问题?像这样构造文本是否可以解决问题? (顺便说一下,向 range() 函数添加第三个参数可以让您定义步长值,而不是默认的 1。)

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

    【讨论】:

    • 我以为我的问题中确实有这样的结构,抱歉。感谢您提供有关第三个参数的提示。我的错误说我试图将一个字符串传递给一个需要整数的方法。错误在第 3 行。是指图片吗?
    • 根据您发布的代码,这将是我能想到的该错误的唯一来源。也许尝试使用 int(pic) 转换为 int?除此之外我真的不知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多