gpt4 book ai didi

python - 在 Python (JES) 中水平翻转图像

转载 作者:行者123 更新时间:2023-11-28 17:46:44 25 4
gpt4 key购买 nike

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

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)

谁能给我解释一下哪里出了问题?谢谢。

最佳答案

试试这个:

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

关于python - 在 Python (JES) 中水平翻转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17129189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com