翻转图像Python

2024-07-04 07:54:43 发布

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

我正试图水平翻转图像。

由此:

Original Pic

对此:

Flipped Pic

但我一直把它镜像到一半。

像这样:

Result I get

我正试图逆转x轴指数,我不明白为什么它会被分割。

def flip(picture):
    height = getHeight(picture)
    width = getWidth(picture)
    newPicture = makeEmptyPicture(width, height)
    x2 = width-1
    for x in range(0, width):
        y2 = 0
        for y in range(0, height):
            pxl = getPixel(picture, x, y)
            newPxl = getPixel(picture, x2, y2)
            color = getColor(pxl)
            setColor(newPxl, color)
            y2 = y2+1
        x2 = x2-1
    return picture

剩下的代码:

def d():    
    f = pickAFile()
    picture = makePicture(f)        
    newPicture = copy(picture)        
    writePictureTo(newPicture, r"D:\FOLDER\newPic4.jpg")
    explore(newPicture)

Tags: infordefrangewidthcolorx2height
3条回答

flip()函数中(就像在任何函数中一样),如其他答案所述,返回picture,这是作为函数参数传递的图像,但在d()中定义。。。

这是一个变量的scope问题,所以我请您再看一下我们讨论的here

在这里,你有两个选择(你在两者之间做了一个融合):

  • 直接修改作为参数给定的picture
  • 创建一个newPicture,修改它,最后返回它

有关二维选项的详细信息:

这里重要的是picture变量属于d()函数(d()是它的作用域)。同时,newPicture变量属于flip()函数(flip()是其作用域)。因此,newPicture的生命周期是flip()(即,在返回时,只要终止flip()函数的执行,它就会被销毁)。而d()对这个新图片一无所知,除非你把它还给d()。

简而言之(假设我们在讨论第二种选择):

1)创建一个以picture作为参数的函数(flip())

2)在flip()中,创建一个局部变量newPicture,并只修改该变量,使原始的picture保持不变

3)将新更新的newPicture返回给父scope。这里d()调用flip(),所以它是父作用域。我们必须创建一个3d变量(属于d()作用域),以掌握flip()返回的内容:

def flip(picture)
    # Create newPicture
    # Modify newPicture (using the information from the "picture" parameter)
    setColor(newPicture, ...)
    ...
    return newPicture

def d():
    file = PickAFile()
    original_pic = makePicture(file) 
    finalNewPicture = flip(original_pic)     # {1}
    show(finalNewPicture)

{1}:这里我们将flip返回的值(即newPicture)赋给更高作用域finalNewPicture的变量(处理程序)。。。

我希望它能帮助你理解这背后的逻辑。就像俄罗斯玩偶:newPicture在flip()中使用,在d()中使用。。。


编辑:

我还想解释一下第一种选择。。。

1)创建一个以picture作为参数的函数(flip())

2)在flip()中,直接修改更大范围的picture变量

3)不要返回flip()中的任何内容

这将导致:

def flip(picture)
    # Simply modify the variable "picture", given as a parameter
    setColor(picture, ...)
    ...
    # Do not return anything

def d():
    file = PickAFile()
    original_pic = makePicture(file) 
    flip(original_pic)                     # {1}
    show(original_pic)

{1}:这里flip()直接对输入图片进行了更改,因此我们可以直接显示原始的修改图片(original_pic)。不需要中间处理程序变量。


选项1的代码:(因为您已经将其用于选项2)

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

  x2=width-1
  for x in range(0, width/2):   # Only process the half way
    y2=0
    for y in range(0, height):
      # swap pix and pix2
      pxl = getPixel(picture, x, y)
      pxl2 = getPixel(picture, x2, y2)
      color = getColor(pxl)
      color2 = getColor(pxl2)
      setColor(pxl2, color)
      setColor(pxl, color2)
      y2=y2+1
    x2=x2-1  

def d():    
  f = pickAFile()
  original_picture = makePicture(f)        
  flip2(original_picture)        
  show(original_picture)

d()

注意:翻转可以被广泛简化如下:

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

  for x in range(0, width/2):   # Only process the half way
    for y in range(0, height):
      # swap pix and pix2
      pxl = getPixel(picture, x, y)
      pxl2 = getPixel(picture, width-1-x, y)
      color = getColor(pxl)
      color2 = getColor(pxl2)
      setColor(pxl2, color)
      setColor(pxl, color2)

您正在更改原始图片中的像素,而不是新创建的新图片。此外,您还将返回原始(现在已修改)图片。

你应该有

newPxl = getPixel(newPicture, x2,y2)

你应该以

return newPicture

另外,我认为您的代码会围绕水平轴翻转图像,而不是像您的图像显示的那样垂直。

def flip(picture):
  height = getHeight(picture)
  width = getWidth(picture)
  newPicture = makeEmptyPicture(width, height)
  for x in range(width):
    for y in range(height):
      color = getColor(getPixel(picture, x, y))
      setColor(getPixel(newPicture, width-1-x, y), color)
  return newPicture

这个怎么样?我刚把那些对我来说不必要的东西去掉了。否则它应该这样工作。我在你的代码中没有发现真正的bug,只是在最后返回了picture,而不是newPicture。但这也不应该导致镜像。

相关问题 更多 >

    热门问题