Jython/Python水平翻转图片

2024-10-01 17:33:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着把一张照片切成两半,然后把两边水平翻转。请参阅下面的链接。在

http://imgur.com/a/FAksh

原图:

enter image description here

输出需要是什么:

enter image description here

我得到了什么

enter image description here

这就是我所拥有的,但它所做的只是水平翻转图片

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)

那么,如何水平翻转每一面,使其看起来像第二张图片?在


Tags: infor水平请参阅图片range照片color
2条回答

一种方法是定义一个用于水平翻转部分图像的函数:

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)

希望这能给你一个开始。在

提示:您可能需要交换两个像素;为此,您需要使用一个临时变量。在

一年后,我想我们可以给出答案:

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条条纹示例:

^{pr2}$

在此之前:

enter image description here

之后:

enter image description here

相关问题 更多 >

    热门问题