python Python图像镜像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1410406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:10:15  来源:igfitidea点击:

Python image mirroring

pythonimage-manipulation

提问by Jafu

I've been making a picture mirroring in horizontal and vertical axes. Now I'm going to make the diagonal.

我一直在制作水平和垂直轴上的图片镜像。现在我要制作对角线。

I had done the hori and verti width two for loops which in the hori scenario loops through all the pixels in the height and only the half of the pixels in the width. Then it gets the color of the pixel and set the same color to the pixel on the other side. Going from the getWidth(pic)to the center.

我已经完成了 hori 和 verti width 两个 for 循环,在 hori 场景中循环遍历高度的所有像素和宽度的一半像素。然后它获取像素的颜色并将相同的颜色设置为另一侧的像素。从getWidth(pic)中心到中心。

Then I have my mirror in the middle of the pic. How to do the diagonal way?

然后我在照片中间放了镜子。对角线怎么做?

Edit:

编辑:

img_src = makePicture(pickAFile())
W = getWidth(img_src)
H = getHeight(img_src)

for x in range(W):
        for y in range(H):
                p = getPixel(img_src, x, y)
                colorInSrc = getColor( getPixel(img_src, x, y) )
                destPixel = getPixel(img_src, H-y-1, W-x-1)
                setColor(destPixel, colorInSrc)

采纳答案by rslite

If I understood correctly what you need is to "flip" the image by a diagonal. Since there are two of them I'll presume that you mean the one that goes from left bottom to right top.

如果我理解正确,您需要的是通过对角线“翻转”图像。由于有两个,我假设您的意思是从左下角到右上角的那个。

In order to flip by this diagonal you need to transform each row from the source in columns in the destination. The left part of the rows will become the bottom part of the new columns. Also the topmost row will become the rightmost column. You will need to do this pixel by pixel on the whole image. Also keep in mind that the width and height of the image will be swapped.

为了通过这条对角线翻转,您需要将源中的每一行转换为目标中的列。行的左侧部分将成为新列的底部。最上面的行也将成为最右边的列。您需要在整个图像上逐个像素地执行此操作。还要记住,图像的宽度和高度将被交换。

Edit: A small example. Say you start with an image 5 pixels wide and 3 pixels high (5x3). You will need to create a new blank image 3 pixels wide and 5 pixels high.

编辑:一个小例子。假设您从一张宽 5 像素、高 3 像素 (5x3) 的图像开始。您需要创建一个新的空白图像,宽 3 像素,高 5 像素。

If you start pixel numbering from left top corner with (0,0), then this pixel will end up at (2,4) in the new image, pixel (1,0) will end at (2,3) and so on.

如果从左上角开始像素编号为 (0,0),那么该像素将在新图像中的 (2,4) 处结束,像素 (1,0) 将在 (2,3) 处结束,依此类推.

If your original width and height are W and H then you should use something like this:

如果您的原始宽度和高度是 W 和 H,那么您应该使用以下内容:

for x in xrange(W):
    for y in xrange(H):
        p = img_src.getpixel(x, y)
        img_dest.setpixel(H-y-1, W-x-1)

This should work, but is not tested.

这应该有效,但没有经过测试。

回答by Paul

Using PIL (the Python Imaging Library) this is a relatively straightforward task. Notice however, that the output image is square -- thus not the same size as the original image.

使用 PIL(Python 成像库)这是一项相对简单的任务。但是请注意,输出图像是方形的——因此与原始图像的大小不同。

Here is the code:

这是代码:

from PIL import Image, ImageDraw

# load the image, create the mirrored image, and the result placeholder
img    = Image.open('img.png')
mirror = img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_90)
sz     = max(img.size + mirror.size)
result = Image.new(img.mode, (sz,sz))
result.paste(img, (0,0)+img.size)

# now paste the mirrored image, but with a triangular binary mask
mask = Image.new('1', mirror.size)
draw = ImageDraw.Draw(mask)
draw.polygon([0,0,0,sz,sz,sz], outline='white', fill='white')
result.paste(mirror, (0,0)+mirror.size, mask)

# clean up and save the result
del mirror, mask, draw
result.save('result.png')

回答by Sven Hecht

It's not really an Python question, is it?

这不是一个真正的 Python 问题,是吗?

The easiest solution would be to first mirror horizontal and then vertical. Another one would be to switch pixel rows with columns.

最简单的解决方案是先水平镜像,然后垂直镜像。另一种方法是用列切换像素行。

Or to do your algorithm but switch the pixels from left-top to bottom-right...

或者做你的算法,但将像素从左上角切换到右下角......

回答by Bart Simpson

Here's how to mirror diagonally in JES; It only works for a square image though:

这是在 JES 中对角镜像的方法;不过,它仅适用于方形图像:

def mirrorDiagonal(picture):
  for sourceX in range(0,getWidth(picture)):
    for sourceY in range (0,getHeight(picture)):
      pex=getPixel(picture,sourceY,sourceX) 
      pix=getPixel(picture, sourceX,sourceY)
      color=getColor(pix)
      setColor(pex,color)
  show(picture)