我想在python中调整图像数据的大小,但简单的numpy.resize
似乎不起作用。
我读取图像,并尝试使用以下脚本调整其大小。为了检查,我写入了导致意外结果的文件。
from PIL import Image
import numpy as np
# Read image data as black-white image
image = Image.open(input_image).convert("L")
arr = np.asarray(image)
# resize image by factor of 2 (original image has shape (834, 1102) )
image = np.resize(arr, (417, 551))
# same resized image to check if it worked
im = Image.fromarray(image)
im.save("test.jpeg")
原始图像:
缩放后的图像:
我希望看到同样的图像(一架飞机),但只是更小,分辨率更小。
我做错了什么?
4条答案
按热度按时间k75qkfdt1#
有多个答案,并且“所有”看起来都有效。我想再添加一个并比较它们:
答案
您可以使用PIL的resize方法:
对比
我以为PIL会更快。
下面是比较这些方法的脚本
1.方法1:@asds_asds的numpy方法。
1.方法2:我提供的PIL的调整大小方法。
1.方法3:@Mark Setchell的方法。
剧本:
这里的结果:
TL;DR
虽然numpy更快,但PIL提供的代码更干净,差距也不是那么明显。所以PIL可能看起来更好...
mnemlml82#
你可以在谷歌上搜索的术语是上采样和下采样。
gg58donl3#
我想就像这个例子我的代码
输出:
标签:
um6iljoc4#
如果你想缩放图像,根本不需要使用Numpy。已经安装的PIL可以做到这一点: