ImageMagick: Performance analysis and optimization
Posted by Jai on October 19, 2012
ImageMagick is widely used command line image manipulation suite. It does provide lot of feature and enables the user with nice image manipulations. If you are planning to use the suite on an environment which is very much processing speed oriented where you need to perform well for each of the image manipulation, we need to analyze the tool settings and optimize it accordingly.
This post will cover how we can analyze ImageMagick from performance perspective and how to optimize it to perform better in different business scenarios.
- ImageMagick Architecture
- ImageMagick Best Practices
- ImageMagick Settings recommendations
- ImageMagick Size/Speed business Approaches
- GraphicsMagick Performance comparison
Configuration file to control settings,
eg. configure.xml, policy.xml needs to be revisited to check the configurations and policy for the environment.
Set the environment settings correctly for the imagemagick,
eg. MAGICK_DISK_LIMIT, MAGICK_MEMORY_LIMIT, MAGICK_THREAD_LIMIT etc. directly control the performance.
OpenMP, is an API specification for parallel programming. If your compiler supports OpenMP (e.g. gcc, Visual Studio 2005) directives, ImageMagick automatically includes support. To verify, look for the OpenMP feature of ImageMagick with this command:
# identify -version Version: ImageMagick 7.0.0-0 2011-12-26 Q16 http://www.imagemagick.org Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC Features: OpenMP OpenCL
For further details on parallel execution with OpenMp, have a look at details.
ImageMagick Best Practices
Architecture best practices section shares some of the best practices to follow for better performance. Some of which are,
- Consider the Q8 version of ImageMagick
- If you plan on processing the same image many times, consider the MPC format.
- If you are dealing with large images, make sure the pixel cache is written to a disk area with plenty of free space.
ImageMagick Settings recommendations
General recommendations to make IM faster, Making IM Faster (in general)
IM is light on CPU, veeeery heavy on RAM. Even if you are using Q16 version you should get some relief with
passing -depth 8 at the beginning of options.
Use MPC for big image file processing or repetitive use of same big image.
Ordering of command line options also effect the processing speed, eg. As mentioned in the post resizing and applying manipulation on a image will be affected by if we do the resize of the image first or later.
To optimize the performance, you should be able to debug the event better. Use “debug” option to Debug the event further.
You can also set the MAGICK_DEBUG environment variable to events eg. CACHE to track if ImageMagick is pushing pixels to disk rather than memory. Keeping pixels in memory is important for speed.
ImageMagick Size/Speed business Approaches
Based on your debug information and your business requirements, you can choose the right strategy which fits your requirements. Some of the examples to share around, the deciding factors for business strategy
- What file size you are dealing with
- What initial file size you will be working on
- What resize file you can have based on requirements, if file will be transferred over net.
- What file format you will be working on PNG/JPG/JPEG/BMP depending on quality, size and processing speed.
- What are common image manipulations operations you will be doing
As shared earlier post “ImageMagick: Choosing the right image format for image manipulation”, you need to choose the right business strategy and accordingly optimize the imagemagick settings also.
GraphicsMagick Performance comparison
GraphicsMagick is the swiss army knife of image processing.
It provides a robust and efficient collection of tools and libraries which support reading, writing, and manipulating an image in over 88 major formats including important formats like DPX, GIF, JPEG, JPEG-2000, PNG, PDF, PNM, and TIFF. Image processing is multi-threaded (see the multi-thread benchmark results) using OpenMP so that CPU-bound tasks scale linearly as processor cores are added.
OpenMP in GraphicsMagick Performance statistics, OpenMP
GraphicsMagick vs ImageMagick Benchmarks are shared here, Benchmarks
One ImageMagick algorithm ran as much as 770 times slower.
For some real example of batch processing of millions of images, here are some statistics.
It is hard to find the solver bullet for performance analysis and optimization, to cover the last mile it is hit and trial approach and the one which fits best to you. All the best!