3

我需要制作一个可以复制图像但镜像的功能。我创建了镜像镜像的代码,但它不起作用,我不知道为什么,因为我跟踪了代码,它应该镜像镜像。这是代码:

def invert(picture):
 width = getWidth(picture)
 height = getHeight(picture)

 for y in range(0, height):
   for x in range(0, width):
    sourcePixel = getPixel(picture, x, y)
    targetPixel = getPixel(picture, width - x - 1, height - y - 1)
    color = getColor(sourcePixel)
    setColor(sourcePixel, getColor(targetPixel))
    setColor(targetPixel, color)
 show(picture)
 return picture 

def main():
  file = pickAFile()
  picture = makePicture(file)
  newPicture = invert(picture)
  show(newPicture)

有人可以向我解释什么是错的吗?谢谢你。

4

2 回答 2

1

问题是您正在循环整个图像而不是宽度的一半。您镜像两次图像并获得与输入图像相同的图像作为输出。

如果您在 Y 轴上镜像,则代码应该是

for y in range(0, height):
for x in range(0, int(width / 2)):
于 2013-06-16T01:08:19.633 回答
1

尝试这个 :

def flip_vert(picture):
    width = getWidth(picture)
    height = getHeight(picture)

    for y in range(0, height/2):
        for x in range(0, width):
            sourcePixel = getPixel(picture, x, y)
            targetPixel = getPixel(picture, x, height - y - 1)
            color = getColor(sourcePixel)
            setColor(sourcePixel, getColor(targetPixel))
            setColor(targetPixel, color)

    return picture 


def flip_horiz(picture):
    width = getWidth(picture)
    height = getHeight(picture)

    for y in range(0, height):
        for x in range(0, width/2):
            sourcePixel = getPixel(picture, x, y)
            targetPixel = getPixel(picture, width - x - 1, y)
            color = getColor(sourcePixel)
            setColor(sourcePixel, getColor(targetPixel))
            setColor(targetPixel, color)

    return picture 
于 2013-06-16T00:49:38.153 回答