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')
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With