我需要创建一个复制图像的函数,但需要镜像。我创建了镜像镜像的代码,但它不起作用,我不知道为什么,因为我跟踪代码,它应该镜像图像。这是代码:
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)
有人可以向我解释有什么问题吗?谢谢。
答案 0 :(得分: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
答案 1 :(得分:1)
问题是你在整个图像中循环而不是只有宽度的一半。您镜像两次您的图像并获得与您输入的图像相同的输出图像。
如果您在Y轴上镜像,则代码应为
for y in range(0, height):
for x in range(0, int(width / 2)):