在Python(JES)中水平翻转图像 [英] Flip horizontally an image in Python (JES)
本文介绍了在Python(JES)中水平翻转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要制作一个可以复制图像但具有镜像功能的函数.我创建了用于镜像图像的代码,但是它不起作用,我也不知道为什么,因为我跟踪了代码,它应该镜像了图像.这是代码:
I need to make a function that will copy an image, but mirrored. I created the code to mirror the image, but it isn't working and I don't know why because I traced the code and it should be mirroring the image. Here's the code:
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)
有人可以向我解释什么地方不对吗?谢谢.
Can someone explain to me what is wrong? Thank you.
推荐答案
尝试一下:
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(JES)中水平翻转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文