Search code examples
imagemagick

The proper order in which ImageMagick command-line options should be specified


In comments to one of my recent ImageMagick questions, Fred Weinhaus noted:

Note that -quality is a setting for the write, not a setting for -rotate, as it comes after -rotate and before the output. (It could come before -rotate only because ImageMagick is somewhat forgiving of syntax errors.)

Source: ImageMagick: The purpose of using '-quality'

Is there a reference (a section at ImageMagick's website, an article, or something similar) that descibes/explains the order in which ImageMagick command-line options should be specified? I mean not the order that "just works" (by forgiving errors, as Fred pointed above), but the order that will really make sense, the proper one.


Solution

  • There is a section in the documents called "Anatomy of a command line" and it shows you the power and available complexity of the ImageMagick command line. It is here.

    Read through till you find Image Operator and Image Setting for a good explanation. Basically, operators do something immediately to any currently loaded images (without affecting any subsequently loaded/created images), whereas settings do not cause any immediate changes/processing to any images, but retain some setting/characteristic/value until needed later - and stay in effect until changed.


    So, for example, -rotate is an operator, so the following command will load two images and rotate them both immediately:

    magick image1.jpg image2.jpg -rotate 45 ...
    

    Whereas, -quality is a setting, so the following will load the same two images and rotate them and you can set quality at any point ahead of the output file and it will affect only the output file quality - not the rotation. All the following are identical:

    magick -quality 3 image1.jpg image2.jpg -rotate 45 result%d.jpg
    magick image1.jpg -quality 3 image2.jpg -rotate 45 result%d.jpg
    magick image1.jpg image2.jpg -quality 3 -rotate 45 result%d.jpg
    magick image1.jpg image2.jpg -rotate 45 -quality 3 result%d.jpg
    

    Another example, -size is a setting so it remains in effect till changed, thereby causing the blue block to have the same size as set for the red one:

    magick -size 32x32 xc:red xc:blue -size 100x32 xc:lime +append result.png
    

    enter image description here

    But -rotate is an operator, so the rotation of the first two blocks is immediate, but doesn't affect the lime-green block which is created afterwards:

    magick -size 32x32 xc:red xc:blue -rotate 45 -size 100x32 xc:lime +append result.png
    

    enter image description here