0

我有我的代码来否定一张照片,但我正在努力弄清楚如何只否定图像的左侧或右侧的一半。下面是负数的代码,我知道我不知道如何获得我需要的一半像素。

def negative(picture):
  for px in getPixels(picture):
      red=getRed(px)
      green=getGreen(px)
      blue=getBlue(px)
      negColor=makeColor(255-red,255-green,255-blue)
      setColor(px, negColor)
4

1 回答 1

0

首先,您需要找到图片的宽度并将其除以 2

halfWidth = getWidth(picture) / 2

因为您不想遍历所有无法使用的像素for px in getPixels(picture):。您需要的是range()与 2 个for循环结合使用的功能,例如

# Iterate along the Y axis
for y in range(0, getHeight(picture)):

  # Iterate along the X axis but only for half the width
  for x in range(0, halfWidth):

    # Get the pixel at X, Y coordinate from picture
    px = getPixel(picture, x ,y)

您的其余代码应与此一起使用。

于 2014-10-24T22:59:41.030 回答