Search code examples
imagemagickimagemagick-convertgnu-parallel

Merging every pair of images with ImageMagick and GNU Parallel


I'm trying to merge every pair of images from a folder and combine that result into a PDF file with imagemagick and parallelize the process with GNU Parallel.

parallel -N2 convert \( {1} -rotate 30 \) {2} +append miff:- ::: *jpeg | convert - out.pdf

The problem is that I need to rotate the first argument and an error occurs.

Error: /bin/sh: -c: line 0: syntax error near unexpected token `01.jpeg'
/bin/sh: -c: line 0: `convert ( 01.jpeg -rotate 30 ) 02.jpeg +append miff:-'
...

How can I process one of the arguments GNU parallel is receiving?


Solution

  • First, you better use parallel -k else the output will be in the wrong order.

    Second, you don't need parentheses to ensure your -rotate only applies to the first image because you haven't loaded a second at that point.

    So, you are looking at:

    parallel -k -N2 convert {1} -rotate 30 {2} +append miff:- ::: *jpeg | convert - out.pdf
    

    or maybe:

    parallel -k -N2 'convert {1} -rotate 30 {2} +append miff:-' ::: *jpeg | convert - out.pdf
    

    In answer to your question, ImageMagick will apply operators (like -rotate) to all images in the loaded stack. So:

    convert im1.png im2.png -rotate ...
    

    will rotate both images. Whereas:

    convert im1.png -rotate 90 im2.png ...
    

    will only rotate the first image. If you want to only rotate the second, you have 2 choices, either put the second in parentheses so the -rotate only applies to it:

    convert im1.png \( im2.png -rotate 90 \) ...
    

    or, load the second image first and rotate it, then load the first and swap the order:

    convert im2.png -rotate 90 im1.png +swap ...