在 Python 中调整大图像大小的 CPU 效率最高的方法是什么

Gun*_*nnm 7 python image python-imaging-library

我正在寻找调整图像大小的最有效方法。如果图像相对较小(例如 3000x2000),PIL 效果很好,但如果分辨率很大(16000x12000),则需要很长时间来处理。图像不必看起来很漂亮我正在调整它们的大小以进行比较以找到带有 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')
Run Code Online (Sandbox Code Playgroud)

tob*_*spr 5

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

im1_resize = img1.resize((640, 480), Image.NEAREST)
Run Code Online (Sandbox Code Playgroud)

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

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

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