Combining ImageMagick import/montage commands into one-liner

61 views Asked by At

Long story short from the command line I need to take a screenshot, crop two areas of that screenshot and join them back together side-by-side.

The separate stages which work fine look like this:

$> import -window root -crop '1280x400+0+0' left.png

$> import -window root -crop '1280x400+0+400' right.png

$> montage -geometry 1280x400 left.png right.png out.png

However, I'm not sure how to combine this into one command.

OS is Linux (Raspbian) if that helps.

1

There are 1 answers

0
Mark Setchell On

I'm not sure how to do any cropping as part of a magick import so I'll import and forward to a regular magick command for processing.

To mimic your approach, that would be:

magick import -window root png:- | magick png:- \
  \( -clone 0 -crop 1280x400+0+0   \) \
  \( -clone 0 -crop 1280x400+0+400 \) \
  -delete 0 +append result.png

That first grabs the screen with import and pipes it on as a PNG to a second command that clones the image and crops the left half, clones it again and crops the right half, deletes the original and places the two crops side-by-side.


There is an easier way that works for you though. If you crop without an offset, all the cropped areas will remain in your stack:

magick import -window root png:- | magick png: -crop x50% +append result.png

Note: I am also using 50% of the height as the crop to save me having to do any maths

Note: If you want a magenta gap between the two halves, add -background magenta near the start and replace +append with +smush 10 to place them 10 pixels apart.