4

I'm creating a process that iterates over an image and does x/y transformations and zooms. The original images are very large. My current process involves making a bash script (using python) to creates rows and rows with the all-famous Imagemagick Convert. The major upside of using convert is the fact that you can load the image into memory once, then write out each frame. However, this bash script workflow is not sustainable, and I need something more native. So this begs the question:

  • What is the FASTEST python processing library for image manipulations? Considering how larger the images can be, speed is the goal.

Here is where my research (and some tinkering) has led me:

Wand (Imagemagick wrapper) - Theoretically, Wand would be great! But I have found no mention of being able to read the image from memory, which makes it VERY slow.

Skimage - I'd love to use Skimage for the depth of the operations (though overkill for my needs), but I've found the API/Documentation very difficult to work with. However, the fact that you can load an image into a numpy array and get breakneck speeds (for Python, anyways), seems like a compelling argument.

PIL - Pil can be very tricky to install, which has caused a lot of trouble on a few systems I'm trying to deploy. The API, though, it's pretty darn easy. I've heard generally to stay away from PIL due to it's ongoing development woes.

Pillow - Similar to above, I've had some trouble getting Pillow working. I think I read that Pillow can conflict with PIL... maybe that's the problem?

I would love some input.

Nae
  • 14,209
  • 7
  • 52
  • 79
M Leonard
  • 581
  • 1
  • 7
  • 23
  • 1
    Installing Pillow is easy and always works: "pip install Pillow". Of course, if your Python instance is system wide, you need to do this as root or admininistrator – trajekolus Nov 02 '17 at 23:57
  • Also, if your Python3 instance has to be called with "python3", you would use "pip3 install Pillow". – trajekolus Nov 03 '17 at 00:04
  • 1
    I have dabbled a little with Python with Skimage and numpy for image processing. It is a bit hard to work, but the speed is great. I have found that PIL (pillow) is a bit limited in functionality. There is also OpenCV, but it is not as flexible as ImageMagick for simple things, but has features not included in ImageMagick, but is faster than ImageMagick. ImageMagick is very flexible to do simple things. There is a Pythonmagick, but it is old. Wand is probably the best for Python ImageMagick type processing. What kind of processing were you doing with convert command? Example? – fmw42 Nov 03 '17 at 00:05
  • 2
    For me skimage is the most natural to work with. Of course my numpy-background plays a role. You did not specify your operations in detail, but skimage sounds like a very good pick, (drop PIL for the more modern Pillow; Pillow is also the easiest to install). OpenCV, mentioned by fmw42 is also a good call for some needs. Not sure if it's for you. It all depends on what you are actually doing (zooming or sub-image indexing should be equally fast for all c-array-based cores; but interpolation/resizing for example may differ). – sascha Nov 03 '17 at 04:21
  • @jdoer1997 I gave Pillow another go. It seemed like as long as I uninstalled PIL, Pillow imported and worked no problem. – M Leonard Nov 03 '17 at 04:31
  • @fmw42 in the grand scheme of things it’s pretty simple stuff. Panning, zooming, rotating. Would skimage be overkill? I had a tough time figuring out which class to use for these simple things, any insight into that? – M Leonard Nov 03 '17 at 04:34
  • All these libraries will have some overhead, doing things you don't need. If speed is what you're optimizing for, you can consider performing some operations on the GPU (OpenGL knows all about image transformations), or you can hand-code transformations using, e.g., numba. It would be good to understand your problem a bit better, because only optimizing for speed is very limiting. – Stefan van der Walt Nov 03 '17 at 04:49
  • You could look at pyvips as well: https://pypi.python.org/pypi/pyvips -- it's a streaming library, and works well with huge (larger than RAM) images. Benchmarks here: https://github.com/jcupitt/libvips/wiki/Speed-and-memory-use – jcupitt Nov 04 '17 at 14:38

1 Answers1

2

This is too long for a single comment. So I am putting it here.

Note that I am not an expert on any of these tools, other than ImageMagick, but have dabbled a little with each.

I welcome comments or opinions from others more expertise than I with these tools

I have not used Wand or PIL much. I don't like PIL since it is not as compatible with numpy without reformatting in my limited experience. So I do not use it. Wand looks good if you like ImageMagick convert. I have used both Skimage and OpenCV for different things. And have mixed them in some Python numpy scripts.

Wand is probably the easiest to use if you know ImageMagick. I do not know much about its speed. See http://docs.wand-py.org/en/0.4.1/wand/image.html. One Wand limitation that I believe exists is that it is missing the ability to deal with profile.

See http://pillow.readthedocs.io/en/3.1.x/reference/Image.html for PIL, which is likely the next easiest to use. But its image format may require format changing to work with numpy. I do not think it is quite as full-functioned as Wand.

Also see http://scikit-image.org/docs/dev/api/skimage.transform.html for rotate, resize and affine transform. It is the next easiest to use but may be faster than the other two.

Also See https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html, though you have to do all that with matrices. It is also very fast and has GPU capabilities.

I have no experience with timing other than I find Skimage and OpenCV faster than Imagemagick. But they are less directly functional for simple things that Imagemagick can do, apart from my comments above.

I think you have to decide what compromise you want with respect to speed and efficiency of coding.

If any one knows of a set of coding and speed tests comparing these tools on simple operations such as resize, rotate, crop, affine, perspective, etc, then that would be useful and I would welcome knowing more about the comparisons.

P.S. I found these references regarding PIL being outdated and superseded by Pillow:

http://pillow.readthedocs.io/en/3.4.x/about.html http://pillow.readthedocs.io/en/3.4.x/installation.html

Also see for docs: https://pypi.python.org/pypi/Pillow/4.3.0 https://pillow.readthedocs.io/en/4.3.x/reference/Image.html

I have not tried the newer version of Pillow.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • 1
    Good summary, thank you. If the images are large, I would **definitely** suggest benchmarking `libvips` - it is very fast and very frugal with memory resources http://jcupitt.github.io/libvips/. See also http://jcupitt.github.io/libvips/API/current/Examples.md.html and https://github.com/jcupitt/libvips/wiki/Speed-and-memory-use – Mark Setchell Nov 03 '17 at 08:56
  • `@Mark Setchell`. Thanks Mark, that is a terrific resource. Unfortunately, I do not see a Python Skimage. I am also surprised that OpenCV 2.4 is not that fast. But perhaps OpenCV 3 is faster. Though one could use GPU with OpenCV. – fmw42 Nov 03 '17 at 16:50
  • The current libvips Python binding is here: https://pypi.python.org/pypi/pyvips just install the libvips shared library, then `pip install pyvips`. – jcupitt Nov 04 '17 at 14:34
  • @fmw42 that test is a bit unfair to OpenCV -- it has lots of image IO, which OpenCV is rather slow at, and little processing, which ocv can do very quickly. Ocv also does not thread for you, so that hurts it badly on that 6 CPU test machine. – jcupitt Nov 04 '17 at 14:36
  • `user894763`. Thanks. I understand. I also note that the versions used there are now old. Second, I suspect that many of the tools only work with images in memory and would fail if the memory is too small for the images. Imagemagick can page to disk, which makes it a bit slow, but will work for very large images. There are probably other things that are in play here also. So I only took that comparison with a grain of salt. But it is useful to see all those tools listed. – fmw42 Nov 04 '17 at 16:30
  • It's the versions shipped with Ubuntu 17.04, so they shouldn't be too old. Some of them are a bit newer (eg. pillow-simd 4.3). I should update for 17.10. – jcupitt Nov 04 '17 at 16:39
  • If an update of John Cupitt's table is going to be made, perhaps add Python, Skimage, (numpy?) combination or have I missed that in your list? Perhaps a fairer test would be to use 1 thread. – fmw42 Nov 04 '17 at 17:47
  • OK, I'll try to add skimage. There's an entry in the table for libvips with one thread, so you can compare that to opencv etc. – jcupitt Nov 24 '17 at 08:57