我需要做一个函数,将复制一个图像,但镜像。我创建了镜像的代码,但它不工作,我不知道为什么,因为我跟踪了代码,它应该镜像镜像。代码如下:
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)有人能给我解释一下出了什么问题吗?谢谢。
发布于 2013-06-16 08:49:38
试试这个:
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-16 09:08:20
问题是你是在整个图像上循环,而不是只有一半的宽度。你镜像你的图像两次,并得到与你输入的图像相同的输出图像。
如果你在Y轴上镜像,代码应该是
for y in range(0, height):
for x in range(0, int(width / 2)):https://stackoverflow.com/questions/17129189
复制相似问题