最有效的Python中调整大图像的CPU方法是什么

2024-06-18 07:36:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找最有效的方式来调整图像大小。如果图像相对较小(例如3000x2000),PIL效果很好,但是如果分辨率很大(1600x12000),则需要较长的时间进行处理。图像不必看起来很漂亮,我正在调整它们的大小以便用nrmse找到图像的副本。在

from PIL import Image

img1 = Image.open("img1.jpg")
img2 = Image.open("img2.jpg")

print img1.size
print img2.size

# add width to height to see which resolution is bigger
im1s = img1.size[0] + img1.size[1]
im2s = img2.size[0] + img2.size[1]

# if both images are bigger than 3000 pixels make them smaller for comparison
if im1s > 3000 and im2s > 3000:
    print("Width and height of both images is bigger than 3000 pixels resizing them for easier comparison")
    im1_resize = img1.resize((640, 480), Image.ANTIALIAS)
    im2_resize = img2.resize((640, 480), Image.ANTIALIAS)

im1_resize.save('im1r.jpg')
im2_resize.save('im2r.jpg')

Tags: to图像imagesizepilisopenjpg
2条回答

向上采样时应传递Image.NEAREST参数,即:

im1_resize = img1.resize((640, 480), Image.NEAREST)

这将只采取最近的像素时,上采样,因此是最快的上采样方法。在

当使用ANTIALIAS时,会对多个像素进行采样以生成调整大小的图像,这要慢得多。在

请注意,最有可能的瓶颈是写出这些文件,而不是升级。在

我有两个建议。一个是libvips,另一个是{a2}。在

因为我没有基准测试,所以我只写下每个库的要点。在

Libvips支持大量的图像格式,并通过智能决策来提高速度,允许多线程处理和使用快速cpu指令explained herebenchmarks。python版本也是here

JPETRAN仅适用于JPEG。它们通过直接操作jpeg数据来获得速度,而无需将主readme中解释的最终输出与基准一起重新压缩。在

我的猜测是,jpegtran是单线程的,当它可以在多处理环境中运行时,它的性能将优于libvip。我们唯一的比较是枕头的基准,他们都比较。Libvips的性能比它高出6倍,而jpetran可能是2倍。在

相关问题 更多 >