首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python (JES)中水平翻转图像

在Python (JES)中水平翻转图像
EN

Stack Overflow用户
提问于 2013-06-16 08:33:03
回答 2查看 14.5K关注 0票数 3

我需要做一个函数,将复制一个图像,但镜像。我创建了镜像的代码,但它不工作,我不知道为什么,因为我跟踪了代码,它应该镜像镜像。代码如下:

代码语言:javascript
复制
def invert(picture):
 width = getWidth(picture)
 height = getHeight(picture)

 for y in range(0, height):
   for x in range(0, width):
    sourcePixel = getPixel(picture, x, y)
    targetPixel = getPixel(picture, width - x - 1, height - y - 1)
    color = getColor(sourcePixel)
    setColor(sourcePixel, getColor(targetPixel))
    setColor(targetPixel, color)
 show(picture)
 return picture 

def main():
  file = pickAFile()
  picture = makePicture(file)
  newPicture = invert(picture)
  show(newPicture)

有人能给我解释一下出了什么问题吗?谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-16 08:49:38

试试这个:

代码语言:javascript
复制
def flip_vert(picture):
    width = getWidth(picture)
    height = getHeight(picture)

    for y in range(0, height/2):
        for x in range(0, width):
            sourcePixel = getPixel(picture, x, y)
            targetPixel = getPixel(picture, x, height - y - 1)
            color = getColor(sourcePixel)
            setColor(sourcePixel, getColor(targetPixel))
            setColor(targetPixel, color)

    return picture 


def flip_horiz(picture):
    width = getWidth(picture)
    height = getHeight(picture)

    for y in range(0, height):
        for x in range(0, width/2):
            sourcePixel = getPixel(picture, x, y)
            targetPixel = getPixel(picture, width - x - 1, y)
            color = getColor(sourcePixel)
            setColor(sourcePixel, getColor(targetPixel))
            setColor(targetPixel, color)

    return picture 
票数 1
EN

Stack Overflow用户

发布于 2013-06-16 09:08:20

问题是你是在整个图像上循环,而不是只有一半的宽度。你镜像你的图像两次,并得到与你输入的图像相同的输出图像。

如果你在Y轴上镜像,代码应该是

代码语言:javascript
复制
for y in range(0, height):
for x in range(0, int(width / 2)):
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17129189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档