Python Imaging Library (Fork)
Go to file
2018-10-05 13:53:08 +03:00
.github Reword 2018-09-18 13:10:34 +03:00
.travis Install CFFI without any PYTHONOPTIMIZE 2018-09-21 13:06:17 +03:00
depends Added raqm scripts and install_extra_test_images script [ci skip] 2018-09-29 14:41:52 +10:00
docs Move "Image size" under "API Changes" 2018-09-30 17:12:20 +03:00
src 5.3.0 version bump 2018-10-01 15:48:10 +10:00
Tests Merge pull request #3203 from radarhere/size 2018-09-30 14:35:35 +03:00
winbuild Fix libtiff build on windows 2018-09-18 12:05:50 -07:00
.appveyor.yml Revert "CI: Temporarily pin pytest to prevent scandir non-compilation" 2018-08-10 20:15:21 +03:00
.codecov.yml Avoid "Missing base report" due to committing CHANGES.rst with "[CI skip]" 2018-01-10 16:33:15 +02:00
.coveragerc Don't exclude Windows code 2014-07-05 21:32:09 +03:00
.editorconfig Remove duplicate line [CI skip] 2016-09-03 12:37:47 +03:00
.gitattributes Set ContainerIO test file as binary 2017-03-26 23:30:05 -07:00
.gitignore Remove additional references to nose tests 2018-09-15 12:29:11 -07:00
.landscape.yaml Set landscape max-line-length 2015-07-21 18:23:55 +10:00
.travis.yml Move PYTHONOPTIMIZE to Python versions which have two jobs 2018-09-21 13:31:16 +03:00
CHANGES.rst 5.3.0 version bump 2018-10-01 15:48:10 +10:00
CHANGES.SIMD.rst fix list (+6 squashed commits) 2018-10-05 13:53:08 +03:00
LICENSE Updated copyright year 2017-12-31 12:05:03 +11:00
Makefile Update Python 2 doc URLs to Python 3 2018-06-09 21:04:34 -07:00
MANIFEST.in Remove redundant travis_after_all stuff 2018-03-21 10:02:42 +02:00
mp_compile.py Fix builds with --parallel 2018-08-05 11:38:22 -04:00
README.md fix list (+6 squashed commits) 2018-10-05 13:53:08 +03:00
README.rst Fixed typo 2018-05-27 12:30:41 +10:00
RELEASING.md Revert "Add updated submodule in pillow-wheels [ci skip]" 2018-08-30 19:21:00 +10:00
requirements.txt Sort requirements.txt alphabetically 2018-01-04 05:24:03 -08:00
selftest.py Prefer more conventional __version__ rather than PILLOW_VERSION 2018-04-25 11:21:38 +03:00
setup.cfg No need to run coverage on dev machines 2017-12-28 14:49:47 +00:00
setup.py compiller flag 2018-10-05 13:53:08 +03:00
tox.ini Support Python 3.7 2018-04-05 13:52:48 +03:00

Pillow-SIMD

Pillow-SIMD is "following" Pillow fork (which is PIL fork itself).

For more information about original Pillow, please read the documentation, check the changelog and find out how to contribute.

Why SIMD

There are many ways to improve the performance of image processing. You can use better algorithms for the same task, you can make better implementation for current algorithms, or you can use more processing unit resources. It is perfect when you can just use more efficient algorithm like when gaussian blur based on convolutions was replaced by sequential box filters. But a number of such improvements are very limited. It is also very tempting to use more processor unit resources (via parallelization) when they are available. But it is handier just to make things faster on the same resources. And that is where SIMD works better.

SIMD stands for "single instruction, multiple data". This is a way to perform same operations against the huge amount of homogeneous data. Modern CPU have different SIMD instructions sets like MMX, SSE-SSE4, AVX, AVX2, AVX512, NEON.

Currently, Pillow-SIMD can be compiled with SSE4 (default) and AVX2 support.

Status

Uploadcare

Pillow-SIMD can be used in production. Pillow-SIMD has been operating on Uploadcare servers for more than 1 year. Uploadcare is SAAS for image storing and processing in the cloud and the main sponsor of Pillow-SIMD project.

Currently, following operations are accelerated:

  • Resize (convolution-based resampling): SSE4, AVX2
  • Gaussian and box blur: SSE4
  • Alpha composition: SSE4, AVX2
  • RGBA → RGBa (alpha premultiplication): SSE4, AVX2
  • RGBa → RGBA (division by alpha): AVX2

See CHANGES.

Benchmarks

The numbers in the table represent processed megapixels of source RGB 2560x1600 image per second. For example, if resize of 2560x1600 image is done in 0.5 seconds, the result will be 8.2 Mpx/s.

  • Skia 53
  • ImageMagick 6.9.3-8 Q8 x86_64
  • Pillow 3.3.0
  • Pillow-SIMD 3.3.0.post1
Operation Filter IM Pillow SIMD SSE4 SIMD AVX2 Skia 53
Resize to 16x16 Bilinear 41.37 337.12 571.67 903.40 809.49
Bicubic 20.58 185.79 305.72 552.85 453.10
Lanczos 14.17 113.27 189.19 355.40 292.57
Resize to 320x180 Bilinear 29.46 209.06 366.33 558.57 592.76
Bicubic 15.75 124.43 224.91 353.53 327.68
Lanczos 10.80 82.25 153.10 244.22 196.92
Resize to 1920x1200 Bilinear 17.80 55.87 131.27 152.11 192.30
Bicubic 9.99 43.64 90.20 112.34 112.84
Lanczos 6.95 34.51 72.55 103.16 104.76
Resize to 7712x4352 Bilinear 2.54 6.71 16.06 20.33 20.58
Bicubic 1.60 5.51 12.65 16.46 16.52
Lanczos 1.09 4.62 9.84 13.38 12.05
Blur 1px 6.60 16.94 35.16
10px 2.28 16.94 35.47
100px 0.34 16.93 35.53

Some conclusion

Pillow is always faster than ImageMagick. And Pillow-SIMD is faster than Pillow in 2—2.5 times. In general, Pillow-SIMD with AVX2 always 8-20 times faster than ImageMagick and almost equal to the Skia results, high-speed graphics library used in Chromium.

Methodology

All tests were performed on Ubuntu 14.04 64-bit running on Intel Core i5 4258U with AVX2 CPU on the single thread.

ImageMagick performance was measured with command-line tool convert with -verbose and -bench arguments. I use command line because I need to test the latest version and this is the easiest way to do that.

All operations produce exactly the same results. Resizing filters compliance:

  • PIL.Image.BILINEAR == Triangle
  • PIL.Image.BICUBIC == Catrom
  • PIL.Image.LANCZOS == Lanczos

In ImageMagick, the radius of gaussian blur is called sigma and the second parameter is called radius. In fact, there should not be additional parameters for gaussian blur, because if the radius is too small, this is not gaussian blur anymore. And if the radius is big this does not give any advantages but makes operation slower. For the test, I set the radius to sigma × 2.5.

Following script was used for testing: https://gist.github.com/homm/f9b8d8a84a57a7e51f9c2a5828e40e63

Why Pillow itself is so fast

There are no cheats. High-quality resize and blur methods are used for all benchmarks. Results are almost pixel-perfect. The difference is only effective algorithms. Resampling in Pillow was rewritten in version 2.7 with minimal usage of floating point numbers, precomputed coefficients and cache-awareness transposition.

Why Pillow-SIMD is even faster

Because of SIMD, of course. There are some ideas how to achieve even better performance.

  • Efficient work with memory Currently, each pixel is read from memory to the SSE register, while every SSE register can handle four pixels at once.
  • Integer-based arithmetic Experiments show that integer-based arithmetic does not affect the quality and increases the performance of non-SIMD code up to 50%.
  • Aligned pixels allocation Well-known that the SIMD load and store commands work better with aligned memory.

Why do not contribute SIMD to the original Pillow

Well, it's not that simple. First of all, Pillow supports a large number of architectures, not only x86. But even for x86 platforms, Pillow is often distributed via precompiled binaries. To integrate SIMD in precompiled binaries we need to do runtime checks of CPU capabilities. To compile the code with runtime checks we need to pass -mavx2 option to the compiler. However this automatically activates all if (__AVX2__) and below conditions. And SIMD instructions under such conditions exist even in standard C library and they do not have any runtime checks. Currently, I don't know how to allow SIMD instructions in the code but do not allow such instructions without runtime checks.

Installation

In general, you need to do pip install pillow-simd as always and if you are using SSE4-capable CPU everything should run smoothly. Do not forget to remove original Pillow package first.

If you want the AVX2-enabled version, you need to pass the additional flag to C compiler. The easiest way to do that is define CC variable while compilation.

$ pip uninstall pillow
$ CC="cc -mavx2" pip install -U --force-reinstall pillow-simd

Contributing to Pillow-SIMD

Pillow-SIMD and Pillow are two separate projects. Please submit bugs and improvements not related to SIMD to original Pillow. All bugs and fixes in Pillow will appear in next Pillow-SIMD version automatically.