I have multiple screenshots, saved as PNG, which I want to batch-crop and merge into a single PDF. Given the required offset and size, cropping is simple enough:
mogrify screenshot01.png -crop 123x456+42+42
This crops a 123×456-pixel image from the screenshot, with its top-left corner 42 pixels from both the top and left edge, and overwrites the existing file. (Note: if you don't want to overwrite your original, use convert
instead of mogrify
.)
Then, merging the screenshots into a single PDF should also be easy:
convert screenshot*.png merged.pdf
...however, this results in a file where all pages are slightly shifted out of view. Why does this happen, and what should I do to prevent it?
When cropping an image, the offset (+42+42 in the example) is stored in the resulting PNG file:
$ identify screenshot01.png
screenshot01.png PNG 123x456+42+42 DirectClass 8-bit 42.4Ki 0.000u 0m:0.000007s
And then, when merging, ImageMagick honours this offset. The offset is also reported by gimp
when opening the file, asking you whether to apply it; other applications like gThumb
silently ignore it, though.
To remove the offset, use the +repage
option, either when cropping...
mogrify screenshot01.png -crop 123x456+42+42 +repage
...or when merging:
convert +repage screenshot*.png merged.pdf