2

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?

F. Zer
  • 1,081
  • 7
  • 9

1 Answers1

1

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 ...
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thank you ! Works perfectly ! Could you explain two things: Why I do not need parentheses? What's the effect of using quotes in your second example? – F. Zer Dec 08 '19 at 21:25
  • 1
    I have added an explanation. Please have another look. – Mark Setchell Dec 08 '19 at 21:41
  • Thank you. When you say, 'put the second in parentheses', I am having an error when used in combination with gnu parallel. I am interested in invoking other commands like `composite` in addition to rotate. That's why I asked. Do you know if it's possible? Something like: `parallel -k -N2 convert \( composite {1} \Test.jpeg -gravity Northwest \) {2} +append miff:- ::: *jpeg | convert - out.pdf`. – F. Zer Dec 08 '19 at 22:01
  • I just found it based on your suggestion. i need to add quotes around the whole command that is going to be used by parallel. – F. Zer Dec 08 '19 at 22:25
  • 1
    Not sure I can give an answer for all cases. Generally, as a first resort, I would try enclosing the ImageMagick stuff in single parentheses, if that doesn't work, try using `parallel --quote` and if that doesn't work, try defining a bash function like this https://stackoverflow.com/a/42029336/2836621 – Mark Setchell Dec 08 '19 at 22:29