在Python中对角镜像 [英] Mirror Image Diagonally in Python

查看:153
本文介绍了在Python中对角镜像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python上的编程类,我们正在通过定义镜像点然后使用嵌套for循环将像素从一侧复制到另一侧来镜像图像。例如,垂直镜像图像将使用以下代码:

I'm taking a programming class on python, and we're working on mirroring images by defining a mirror point and then copying a pixel from one side to the other using nested for loops. For example, mirroring an image vertically would use the following code:

def mirrorVertical(source):
 mirrorPoint = getWidth(source) / 2
 width = getWidth(source)
 for y in range(0,getHeight(source)):
   for x in range(0,mirrorPoint):
     leftPixel = getPixel(source,x,y)
     rightPixel = getPixel(source,width - x - 1,y)
     color = getColor(leftPixel)
     setColor(rightPixel,color)

我正在处理一个任务问题,要求我们对角镜像,以便左上角得到反映在右下方。到目前为止,我发现的每个示例和答案仅适用于方形图像,我需要能够将其应用于任何图像,最好是通过定义对角镜像点。我一直在尝试使用y = mx + b样式方程定义镜像点,但我无法弄清楚如何告诉Python使这一行成为一条线。任何不特定于方形图像的帮助都将受到赞赏!

I'm currently working on an assignment question asking us to mirror an image diagonally so that the top left side gets reflected onto the bottom right side. Every example and answer that I've found so far only works for square images, and I need to be able to apply this to any image, preferably by defining a diagonal mirror point. I've been trying to define the mirror point using a y = mx + b style equation, but I can't figure out how to tell Python to make that a line. Any help not specific to square images would be appreciated!

注意:因为我在这里是全新的,我还不能发布图像,但对角线镜像点会从左下角到右上角。左上角三角形中的图像将反映到右下角。

Note: since I'm brand new here, I can't post images yet, but the diagonal mirror point would run from the bottom left to the top right. The image in the top left triangle would be reflected to the bottom right.

推荐答案

您可以将左上角与左下角交换像这样的非正方形数组:

You can swap upper left with lower right of a non-square array like this:

height = getHeight(source)
width = getWidth(source)
for i in range(height - 1):
    for j in range(int(width * float(height - i) / height)):
        # Swap pixel i,j with j,i

这适用于沿对角线镜像。您似乎暗示您可能想要沿某个任意位置镜像。在这种情况下,您需要决定如何填充镜像线另一侧没有匹配像素的像素。

This works for mirroring along the diagonal. You appear to imply that you may want to mirror along some arbitrary location. In that case, you will need to decide how to fill in pixels that don't have a matching pixel on the opposite side of the mirror line.

你提到的是处理任务所以你可能需要明确地执行循环,但请注意,如果你将数据放入一个numpy数组,你可以通过numpy函数的组合轻松(并且更有效地)实现你想要的东西 fliplr flipud transpose

You mention you are working on an assignment so you probably are required to do the loops explicitly but note that if you put your data into a numpy array, you can easily (and more efficiently) achieve what you want with a combination of the numpy functions fliplr, flipud, and transpose.

另请注意,在您的代码示例中(镜像左/右),您将左侧像素复制到右侧,但反之亦然,因此您实际上并未镜像图像。

Note also that in your code example (mirroring left/right), you are copying the left pixel to the right side, but not vice versa so you are not actually mirroring the image.

这篇关于在Python中对角镜像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆