
That’s called “nearest neighbor interpolation.” Nearest neighbor interpolation The easiest way is to assign that nearest value to the output. Taking the factor into consideration, we can always calculate which pixel from the source image is “closer” to the output pixel. That’s where a “scaling factor” comes in, a ratio of input image dimensions to output image dimensions. In the very beginning, the output is an empty matrix and we’re calculating values to put into its cells. Let me try and explain how convolution works when resizing images. No tricks like decoding a smaller image from a JPEG, no algorithm combinations, just assessing the speed of convolution resampling calculations. We do account for memory allocation and calculating coefficients needed for operations. All the calculations use data stored in RAM, so we aren’t taking coding/decoding procedures into account. The resampling itself is performed on arrays of 8-bit RGB pixels. The drawback of keeping the original quality is the poor performance of that algorithm.

It helps preserve the quality of the source image. In many cases, the most suitable way to do that would be convolution-based resampling. It’s an operation aimed at changing the dimensions of a source image. And, in the age of responsive designs, you want more. This allows to process about 2.5k photos per hour on a single core. The taskĮntry conditions are as follows: a photo shot with the latest iPhone takes about 1.37 s to get resized to 2 MP via Pillow 2.6 with none of the optimizations implemented. That’s also a part of what I do, so you can always check out the testing results, submit your own data or run your own tests. When someone claims he made something unique, there’s always a question of testing.

#Best image resizer python code
And that’s the thing, really: you can optimize the code well only when you’re aware of the architecture of a device running it. The lib is also faster than Intel Performance Primitives, low-level building blocks for image processing optimized for a wide range of Intel architectures. On average, Pillow-SIMD is currently resizing images 15 times faster than ImageMagick. I forked Pillow, a Python image processing library used by Uploadcare, and called the fork Pillow-SIMD. Right when I started off, we decided to open the source code. With optimizations, Uploadcare now needs six times fewer servers to handle its load than before. I wasn’t building it for fun: I work for Uploadcare and resizing images has always been a practical issue with on-the-fly image processing. I call it “the fastest resize” because every other library I could find and test was slower. Part 0 is intended to give you the big picture. It doesn’t seem a single article has enough room for that, so I’ll be releasing more. However, I love tech details, so I’m planning to talk over plenty of those. My warmest thanks to fellow Pillow contributors for their comments and motivation: Alex Clark, Hugo van Kemenade, Andrew MurrayĮven though this article is mostly for developers, I seek to keep it simple. And, I’d like to share my experience hoping I might motivate and inspire you to go optimize things. I made the fastest image resize for modern x86 processors. If you save the above program as Example.Hi there. #Make the new image half the width and half the height of the original image The program for resizing and saving the resized image is given below − To resize an image, you call the resize() method of pillow’s image class by giving width and height. This tuple consists of width and height of the image as its elements. The Image module from pillow library has an attribute size. Most of the digital image is a two-dimensional plane of pixels and it has a width and height.
