Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most CPU efficient way to resize big images in Python

I'm looking for most efficient way to resize images. PIL works good if images are relatively small (for example 3000x2000) but if resolution is big (16000x12000) it takes long time to process. Images don't have to look pretty I'm resizing them for comparison to find copies of image with 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')
like image 373
Gunnm Avatar asked Apr 24 '16 07:04

Gunnm


2 Answers

You should pass the Image.NEAREST parameter when upsampling, i.e.:

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

This will only take the closest pixel when upsampling, and thus is the fastest upsampling method.

When using ANTIALIAS multiple pixels are sampled to produce the resized image, which is much slower.

Note that most likely your bottleneck is writing out those files, and not upscaling though.

like image 155
tobspr Avatar answered Nov 15 '22 10:11

tobspr


I have two recommendations. One is libvips and the other is jpegtran-cffi.

Since I did no benchmarking I just write down the points of each library.

Libvips supports a large range of image formats and gains its speed by smart decisions to allow multithreading and use of fast cpu instructions explained here and benchmarks. Also python version is here

Jpegtran works only on jpegs. They get their speed by directly operating on the jpeg data without recompressing the final output explained in the main readme together with a benchmark.

My guess is, that jpegtran is single threaded and would outperform libvip when it can run in a multiprocessing environment. The only comparison we have is the benchmark of pillow to which they both compare. Libvips outperforms it by a factor of 6 and jpegtran maybe by a factor of 2.

like image 37
balrok Avatar answered Nov 15 '22 11:11

balrok