【问题标题】:Python function to mirror an image vertically from R to LPython函数将图像从R垂直镜像到L
【发布时间】:2012-10-09 20:12:37
【问题描述】:

我正在尝试编写一个 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)

【问题讨论】:

    标签: python function mirror


    【解决方案1】:
      color = getColor(rightPixel)
      setColor(leftPixel,color)
    

    【讨论】:

      【解决方案2】:

      您似乎是从左上角迭代到中间,而不是从右上角迭代到中间。可能想为 x 尝试 range(getWidth(), mirrorPoint) 并让 y 保持不变。

      【讨论】:

        【解决方案3】:

        在更改rightPixel 的颜色之前,您应该将此颜色保存到某个位置,以便在leftPixel 上设置它。

        类似

        color_left = getColor(leftPixel)
        color_right = getColor(rightPixel)
        setColor(leftPixel, color_right)
        setColor(rightPixel, color_left)
        

        应该可以解决问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-07
          • 2018-04-10
          • 2017-11-11
          • 2015-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-05
          相关资源
          最近更新 更多