我正在尝试编写一个python函数来将图片的右半部分镜像到左半部分。 到目前为止,我有这段代码,但它以相反的方式工作(它从 L 镜像到 R)我知道它一定是一些简单的更改,但现在我似乎有一个块。 任何帮助表示赞赏。
def mirrorVertical(source):
mirrorPoint = getWidth(source) / 2
width = getWidth(source)
for y in range(0,getHeight(source)):
for x in range(0,mirrorPoint):
leftPixel = getPixel(source,x,y)
rightPixel = getPixel(source,width - x - 1,y)
color = getColor(leftPixel)
setColor(rightPixel,color)
color = getColor(rightPixel)
setColor(leftPixel,color)
看起来您正在从左上角迭代到中间,而不是从右上角迭代到中间。可能想尝试 range(getWidth(), mirrorPoint) 对于 x 并保持 y 不变。
在更改rightPixel
的颜色之前,您应该将此颜色保存到某个地方,以便在leftPixel
上设置它。
类似的东西
color_left = getColor(leftPixel)
color_right = getColor(rightPixel)
setColor(leftPixel, color_right)
setColor(rightPixel, color_left)
应该做这个伎俩。