Python图像反转
我正在尝试把一张图片完全翻转,也就是从左边变到右边,但我似乎找不到合适的代码来实现这个功能。我知道怎么把图片镜像一半,但要完全翻转却让我感到困惑。目前我已经有了以下的代码:
def flip(source):
width=getWidth(source)/2
height=getHeight(source)
for y in range(0,height):
for x in range(0,width):
leftPixel=getPixel(source,x,y)
rightPixel=getPixel(source,width-x-1,y)
color1=getColor(leftPixel)
color2=getColor(rightPixel)
setColor(rightPixel,color1)
setColor(leftPixel,color2)
3 个回答
0
你的代码运行起来会比ImageOps.mirror()这个函数要慢很多,而这个函数是Python图像库的一部分。
1
rightPixel=getPixel(source,width-x-1,y)
width = getWidth (source)
height = getHeight(source)
for y in range(height):
for x in range(width / 2):
在这一行中,width
应该是图片的完整宽度,而不是一半的宽度。我建议把 /2
移到内部循环的范围里。