翻转图像Python

时间:2013-07-03 11:32:39

标签: python image jython image-rotation jes

我试图水平翻转图像。

由此:

Original Pic

To This:

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)

4 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:6)

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

你应该

newPxl = getPixel(newPicture, x2,y2)

你应该以

结束
return newPicture

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

答案 2 :(得分:4)

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

这是scope变量的问题,所以我邀请您再次查看我们的讨论here

在这里,你有两个选择(你在两者之间融化):

  • 直接修改picture作为参数
  • 创建newPicture,修改它,最后将其返回

有关第二个选项的详细信息:

这里重要的是picture变量属于d()函数(d()是它的范围)。同时newPicture变量属于flip()函数(flip()是其范围)。因此newPicture的生命周期是flip()(即,一旦你终止flip()函数的执行,它就会被破坏,同时返回)。并且d()对这个newPicture一无所知,除非你将它返回到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()

注意: flip可以大致简化如下:

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)

答案 3 :(得分:3)

  

OpenCV提供了翻转图像的功能。

void flip(array src, array dst, int flipCode)

围绕垂直,水平或两个轴翻转2D阵列。

参数:

  • src - 源数组
  • dst - 目标数组;将与src
  • 具有相同的大小和类型
  • flipCode - 指定如何翻转数组:0表示绕x轴翻转,正(例如1)表示绕y轴翻转,负(例如-1)表示翻转两者axis。函数以三种不同的方式翻转数组(行和列索引从0开始)。

示例代码:

cv.flip(original_image,flipped_image,1);

编辑:您还可以指定翻转图像:

flipped_image = cv2.flip(src=original_image, flipCode=1)
相关问题