我试图将一张图片“剪切”成两半并水平翻转两边。见下面的链接。
原始图片:
输出需要是什么:
我得到了什么
这就是我所拥有的,但它所做的只是水平翻转图片
def mirrorHorizontal(picture):
mirrorPoint = getHeight(picture)/2
height = getHeight(picture)
for x in range(0, getWidth(picture)):
for y in range(0, mirrorPoint):
topPixel = getPixel(picture, x, y)
bottomPixel = getPixel(picture, x, height - y - 1)
color = getColor(topPixel)
setColor(bottomPixel, color)
那么我如何水平翻转每一面,使它看起来像第二张图片?
答案 0 :(得分:1)
一种方法是定义一个水平翻转部分图像的功能:
def mirrorRowsHorizontal(picture, y_start, y_end):
''' Flip the rows from y_start to y_end in place. '''
# WRITE ME!
def mirrorHorizontal(picture):
h = getHeight(picture)
mirrorRowsHorizontal(picture, 0, h/2)
mirrorRowsHorizontal(picture, h/2, h)
希望这能为你提供一个开始。
提示:您可能需要交换两个像素;为此,您需要使用临时变量。
答案 1 :(得分:0)
一年后,我想我们可以给出答案:
def mirrorRowsHorizontal(picture, y_start, y_end):
width = getWidth(picture)
for y in range(y_start/2, y_end/2):
for x in range(0, width):
sourcePixel = getPixel(picture, x, y_start/2 + y)
targetPixel = getPixel(picture, x, y_start/2 + y_end - y - 1)
color = getColor(sourcePixel)
setColor(sourcePixel, getColor(targetPixel))
setColor(targetPixel, color)
def mirrorHorizontal(picture):
h = getHeight(picture)
mirrorRowsHorizontal(picture, 0, h/2)
mirrorRowsHorizontal(picture, h/2, h)
取自垂直翻转here。
3条纹示例:
mirrorRowsHorizontal(picture, 0, h/3)
mirrorRowsHorizontal(picture, h/3, 2*h/3)
mirrorRowsHorizontal(picture, 2*h/3, h)
之前:
之后: