Python图像镜像
我一直在制作水平轴和垂直轴镜像的图片。现在我要做对角线。
我已经完成了 hori 和 verti width 两个 for 循环,在 hori 场景中循环遍历高度中的所有像素,而仅循环宽度中的一半像素。然后它获取像素的颜色并将相同的颜色设置给另一侧的像素。从 getWidth(pic)
到中心。
然后我把镜子放在照片的中间。对角线怎么做?
编辑:
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)
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.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 PIL(Python 成像库)这是一项相对简单的任务。但请注意,输出图像是正方形的——因此与原始图像的大小不同。
这是代码:
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.
Here is the code:
如果我理解正确的话,你需要的是将图像“翻转”对角线。由于有两个,我假设您指的是从左下到右上的那个。
为了按这条对角线翻转,您需要将源中的每一行转换为目标中的列。行的左侧部分将成为新列的底部。最上面的行也将成为最右边的列。您需要在整个图像上逐像素地执行此操作。另请记住,图像的宽度和高度将被交换。
编辑:一个小例子。假设您从 5 像素宽、3 像素高 (5x3) 的图像开始。您需要创建一个新的空白图像,宽 3 像素,高 5 像素。
如果从左上角开始对像素进行编号 (0,0),则该像素将在新图像中的 (2,4) 处结束,像素 (1,0) 将在 (2,3) 处结束,依此类推。
如果您的原始宽度和高度是 W 和 H,那么您应该使用类似这样的内容:
这应该可以工作,但未经测试。
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.
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.
If your original width and height are W and H then you should use something like this:
This should work, but is not tested.
这并不是一个真正的 Python 问题,不是吗?
最简单的解决方案是先水平镜像,然后垂直镜像。
另一种方法是切换像素行和列。
或者执行您的算法,但将像素从左上角切换到右下角......
It's not really an Python question, is it?
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...
以下是如何在 JES 中进行对角镜像;但它仅适用于方形图像:
Here's how to mirror diagonally in JES; It only works for a square image though: