翻转图像Python

Yve*_*omb 4 python jython image image-rotation jes

我试图水平翻转图像.

由此:

原始图片

对此:

翻转图片

但我一直在镜像中反映它.

像这样:

结果我明白了

我试图扭转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
Run Code Online (Sandbox Code Playgroud)

我剩下的代码:

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

Alf*_*lfe 6

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
Run Code Online (Sandbox Code Playgroud)

这个怎么样?我只是删除了x2/ y2东西,这似乎没有必要给我.否则它应该这样工作.我发现代码中没有真正的错误,只是返回picture而不是newPicture最后.但这不应该导致镜像.


Rog*_*sjö 6

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

你应该有

newPxl = getPixel(newPicture, x2,y2)
Run Code Online (Sandbox Code Playgroud)

你最后应该结束

return newPicture
Run Code Online (Sandbox Code Playgroud)

此外,我认为您的代码会围绕水平轴翻转图像,而不是图像显示的垂直方向.


Gau*_*lio 5

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

这是scope变量的问题,因此我邀请您再次看看我们在这里进行的讨论。

在这里,您有两个选择(您在两个选择之间进行了融合):

  • 直接修改picture给定的参数
  • 创建一个newPicture,对其进行修改,最后返回

有关2d选项的详细信息:

这里重要的是picture变量属于d()函数(d()是函数的范围)。同时,newPicture变量属于flip()函数(flip()是其作用域)。因此,newPicture的生命周期为flip()(即,一旦您终止执行flip()函数并返回时,它就会被销毁)。除非您将其返回给d(),否则d()对此newPicture一无所知。

因此,简而言之(假设我们正在谈论第二种选择):

1)创建一个以a 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)
Run Code Online (Sandbox Code Playgroud)

{1}:在这里,我们将翻转(即newPicture)返回的值分配给范围更大的变量finalNewPicture(处理程序)...

我希望它可以帮助您了解其背后的逻辑。就像俄罗斯玩偶一样:newPicture用于flip()内部,而d()内部则使用...


编辑:

我也想对第一个选项做一个解释...

1)创建一个以a 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)
Run Code Online (Sandbox Code Playgroud)

{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()
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)