PIL:叠加不同尺寸和纵横比的图像 [英] PIL: overlaying images with different dimensions and aspect ratios

查看:71
本文介绍了PIL:叠加不同尺寸和纵横比的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在 python 中叠加两个图像以匹配坐标,左上角和右下角具有相同的坐标,并且它们的方面几乎相同,只有几个像素.虽然它们的分辨率不同.

I've been attempting to overlay two images in python to match coordinates, the top left and bottom right corners have the same coordinates and their aspects are almost identical bar a few pixels. Although they are different resolutions.

使用 PIL 我已经能够覆盖图像,虽然在覆盖它们之后图像输出是方形但分辨率是背景图像的分辨率,前景图像也被错误地调整大小(据我所知).我一定是做错了什么.

Using PIL I have been able to overlay the images, though after overlaying them the image output is square but the resolution is that of the background image, the foreground image is also re-sized incorrectly (As far as I can see). I must be doing something wrong.

import Image
from PIL import Image

#load images
background = Image.open('ndvi.png')
foreground = Image.open('out.png')

#resizing
foreground.thumbnail((643,597),Image.ANTIALIAS)
#overlay
background.paste(foreground, (0, 0), foreground)
#save
background.save("overlay.png")
#display
background.show()

当将图像放入诸如 powerpoint 之类的可怕内容时,图像方面几乎相同.我已经包含了一个示例图像,左边的图像是我手工叠加的,右边的图像是 python 的输出.代码中某个点的背景被垂直挤压,也会影响覆盖.我希望能够在 python 中执行此操作并使其正确看起来像左手图像.

When dropping the images into something horrible like powerpoint the image aspects are almost identical. I've included an example image, the image on the left is my by hand overlay and the image on the right is the output from python. The background at some point in the code is squashed vertically, also affecting the overlay. I'd like to be able to do this in python and make it correctly look like the left hand image.

推荐答案

预先解决方案.

宽/高/比例:300/375/0.800

width/height/ratio: 300 / 375 / 0.800

宽/高/比例:400/464/0.862

width/height/ratio: 400 / 464 / 0.862

from PIL import Image

imbg = Image.open("bg.png")
imfg = Image.open("fg.png")
imbg_width, imbg_height = imbg.size
imfg_resized = imfg.resize((imbg_width, imbg_height), Image.LANCZOS)
imbg.paste(imfg_resized, None, imfg_resized)
imbg.save("overlay.png")

您在问题中提供的最重要信息是:

The most important information you have given in your question were:

  • 前景和背景图像的纵横比相等,但相似
  • 最后需要将两张图片的左上角和右下角对齐.

从这些点得出的结论是:其中一张图像的纵横比必须改变.这可以通过 resize() 方法来实现(而不是使用 thumbnail(),如下所述).总而言之,目标很简单:

The conclusion from these points is: the aspect ratio of one of the images has to change. This can be achieved with the resize() method (not with thumbnail(), as explained below). To summarize, the goal simply is:

将具有较大尺寸的图像(前景图像)调整为较小背景图像的精确尺寸.也就是说,不一定要保持前景图像的纵横比.

Resize the image with larger dimensions (foreground image) to the exact dimensions of the smaller background image. That is, do not necessarily maintain the aspect ratio of the foreground image.

这就是上面的代码所做的.

That is what the code above is doing.

关于你的方法的两条评论:

Two comments on your approach:

首先,我推荐使用最新版本的 Pillow(Pillow 是 PIL 的延续项目,它是 API 兼容的).在 2.7 版本中他们大大提高了图像重新缩放质量.该文档可以在 http://pillow.readthedocs.org/en/latest/reference.

First of all, I recommend using the newest release of Pillow (Pillow is the continuation project of PIL, it is API-compatible). In the 2.7 release they have largely improved the image re-scaling quality. The documentation can be found at http://pillow.readthedocs.org/en/latest/reference.

然后,您显然需要控制两个图像的纵横比如何在整个程序中演变.例如,thumbnail() 不会改变图像的纵横比,即使您的 size 元组没有相同的纵横比作为原始图像.引用自 thumbnail() 文档:

Then, you obviously need to take control of how the aspect ratio of both images evolves throughout your program. thumbnail(), for instance, does not alter the aspect ratio of the image, even if your size tuple does not have the same aspect ratio as the original image. Quote from the thumbnail() docs:

此方法修改图像以包含缩略图版本本身,不大于给定的大小.该方法计算一个适当的缩略图大小以保留图像的外观

This method modifies the image to contain a thumbnail version of itself, no larger than the given size. This method calculates an appropriate thumbnail size to preserve the aspect of the image

所以,我不确定您使用 (643,597) 元组的确切位置,以及您之后是否可能依赖缩略图来获得此确切大小.

So, I am not sure where you were going exactly with your (643,597) tuple and if you are possibly relying on the thumbnail to have this exact size afterwards.

这篇关于PIL:叠加不同尺寸和纵横比的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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