.. title: ImageMagick reminder .. slug: imagemagick-reminder .. date: 2017-12-24 12:44:48 UTC+01:00 .. tags: imagemagick, bash .. category: blog .. link: .. description: .. type: text ImageMagick allows you to realise many tasks to modify or convert images. It is not always easy to remember all the fitting commands to use this Swiss army knife of image manipulation; hence this reminder. .. TEASER_END ``mogrify`` *vs* ``convert`` ============================ Both commands can be used almost equally, but ``mogrify`` is better fitted to bulk modifications whereas ``convert`` is better adapted to single file modifications. It is nonetheless possible to do everything with both commands, as shown below. Resize images ============= It is rather easy to resize a bunch of images to harmonize their size (and converting them, if needed, in an other format) with ``-resize`` option. To get images with a constant height, use ``-resize x[height]``; for a constant width, use ``-resize [width]x``, with dimensions given in pixels. It is also possible to keep the name of the file using ``-set filename:base %[base]``. Here is an example to convert images with a constant height of 100 pixels: .. code-block:: bash convert ../{image-pattern} -resize x100 -set filename:base "%[base]" "%[filename:base]_100.png" File conversion =============== ``mogrify`` and ``convert`` can be used to convert files easily. To modify image quality, it is possible to play with resolution in dpi with ``density`` option (set to 72 by default). Here is an example to convert EPS files to PNG: .. code-block:: bash mogrify -format png -density 150 *eps Animations ========== ImageMagick is useful to create animations — as animated GIF — from a bunch of images. You can set a delay between each frame (and therefore define something like a framerate) with ``-delay`` option. Here is a simple example: .. code-block:: bash convert -delay 20 $(ls *png) animation.gif And if you wish to realise an animation with a selection on files: .. code-block:: bash convert -delay 20 $(ls *png | head -n40) animation.gif Remove images from an animation =============================== First step is to retrieve images from the animation: .. code-block:: bash convert animation.gif +adjoin temp_%02d.gif Then, you can select every ``n`` frames with a ``for`` loop on all the images. For example, you can check that the iterator is a multiple of 2, and if it is true, to copy it in a new file: .. code-block:: bash j=0; for i in $(ls temp_*gif); do if [ $(( $j%2 )) -eq 0 ]; then cp $i sel_`printf %02d $j`.gif; fi; j=$(echo "$j+1" | bc); done If, on the contrary, you wih to keep all the files that are **not** divisible by 2, you can replace ``-eq`` by ``-ne``. Lastly, you can create the new animation: .. code-block:: bash convert -delay 20 $( ls sel_*) new_animation.gif