所以我需要镜像一个图像。应将图像的右上侧翻转到左下方。我创建了一个功能,将图像的左上角翻转到右下角,但我似乎无法弄清楚如何以另一种方式进行操作。这是代码:
def mirrorPicture(picture):
height = getHeight(canvas)
width = height
# to make mirroring easier, let us make it a square with odd number
# of rows and columns
if (height % 2 == 0):
height = width = height -1 # let us make the height and width odd
maxHeight = height - 1
maxWidth = width - 1
for y in range(0, maxWidth):
for x in range(0, maxHeight - y):
sourcePixel = getPixel(canvas, x, y)
targetPixel = getPixel(canvas, maxWidth - y, maxWidth - x)
color = getColor(sourcePixel)
setColor(targetPixel, color)
return canvas
不过,我正在使用名为“JES”的IDE。
答案 0 :(得分:2)
如果通过“镜像”,你的意思是“对角翻转”,这应该有效:
def mirrorPicture(picture):
height = getHeight(picture)
width = getWidth(picture)
newPicture = makeEmptyPicture(height, width)
for x in range(0, width):
for y in range(0, height):
sourcePixel = getPixel(picture, x, y)
targetPixel = getPixel(newPicture, y, x)
# ^^^^ (simply invert x and y)
color = getColor(sourcePixel)
setColor(targetPixel, color)
return newPicture
给予:
.................. ........................ ...... .................
关于对角镜像的相关答案here。