python - 在 python PIL 中使用三次插值放大图像

标签 python image resize python-imaging-library interpolation

您好,我只是想将一批大小为 (a,b,3) 的图像调整为更大的 (c, d, 3) >(c>a, d>b) 使用三次插值。但是每当我一开始似乎成功调整大小后再次打开调整大小的图像时,我发现了旧的尺寸......它发生在我试验中的每张图像和每一个尺寸......任何人都可以指出我遗漏了什么吗?非常感谢!

这是我的代码:

from PIL import Image
im = Image.open("img0.jpg").convert("RGB")
im # the original size
<PIL.Image.Image image mode=RGB size=600x337 at 0x102D83450>
im.resize((800,400),Image.BICUBIC)
<PIL.Image.Image image mode=RGB size=800x400 at 0x102D834D0> # thought I was doing it right
im.save("resized.jpg")
im=Image.open("resized.jpg").convert("RGB")
im
<PIL.Image.Image image mode=RGB size=600x337 at 0x102D83490> # and the actual size seems even smaller than before!

最佳答案

图像大小调整不会就地发生。将返回一个调整大小的新图像,因此您必须保存它。

new_img = im.resize((800,400),Image.BICUBIC)
new_img.save("resized.jpg")

im.resize((800,400),Image.BICUBIC).save("resized.jpg")

方法或函数是否“就地”进行更改(这意味着没有可获取和使用的返回值,并且返回 None 值)或返回必须使用的值取决于创建者方法或功能。您始终可以通过反复试验来解决这个问题,但更好的方法是查看文档。比如PIL/Pillow的resize()方法,看https://pillow.readthedocs.io/en/3.4.x/reference/Image.html#PIL.Image.Image.resize 在那里你会看到函数

Returns a resized copy of this image.

这告诉您必须对返回值做一些事情才能保持方法的效果。

此外,如果您转到 http://effbot.org/imagingbook/image.htm然后跳转到 resize,你会看到它说:

resize #

im.resize(size) ⇒ image

im.resize(size, filter) ⇒ image

指向右边的“箭头”是表示该方法返回一个值的符号。在这种情况下,它会返回一个图像。

关于python - 在 python PIL 中使用三次插值放大图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47763704/

相关文章:

python - DjangoModelPermissions 和 DjangoObjectPermissions 之间有什么区别

python - Jython : SyntaxError: invalid syntax

java - 如何将图像高度调整为 JFrame 高度?

iphone - sizeThatFits 的正确使用方法 : and sizeToFit with UILabel?

Android:调整 Horizo​​ntalScrollView 的大小

.net - 大型图像调整大小库

python - 使用python请求库动态获取HTTP错误字符串

python - 如何使用任何语言 C++、Python、Java、C 在两个客户端之间传递套接字对象

jquery - mask 图像对于 div css 来说太大了

Android - 按钮按下事件上的图像 - 将图像叠加应用于所有