ajeh
ajeh

Reputation: 2784

Cropping images with ImageMagic produces wrong size images - why?

There is a bunch of images that represent the pages, scanned from a book and already cropped with ImageMagic. They are all uniform, 629x625 pixels. I need to process them further to prepare for creation of a PDF: split them into odd and even pages. They are named canvas-00.png through canvas-53.png

When I use the following command to get the odd pages first, I get completely butchered images 275x563 pixels extracted from a wrong horizontal offset:

convert canvas-??.png -crop 314x625+0+0 ~/Pictures/odd/canvas.png

What am I doing wrong?

PS: Originally the images were larger, contained extra margins with scanner's lid visible, and I successfully cropped them to 629x625 using a very similar command, but of course with non-zero offset of +140+71. So it baffles me that a visibly more trivial command with zero offset does not work at all.

Upvotes: 2

Views: 1786

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208107

I suspect you have page offsets "remembered" in your PNG files and you need to clear them.

As an example, let's create an image 500x300 pixels:

convert xc:red xc:lime +append \
   \( xc:blue xc:magenta +append \) -append -resize 500x300\! start.png

enter image description here

Now let's crop out a piece towards the bottom right corner, which I intentionally made magenta so you can see which piece I got:

convert start.png -crop 200x100+250+150 z.png

enter image description here

But if we now look at it with identify you can see it "remembers" where it was in the original picture:

identify z.png

Output

z.png PNG 200x100 500x300+250+150 16-bit sRGB 51.1KB 0.000u 0:00.000

This will then potentially affect (i.e. mess up) anything you do in future with the image. The solution is to use +repage to reset all the page geometry and layout so it forgets. You can either do this when cropping, or when using the cropped image later - depending whether you want to retain the crop information or not. (Sometimes you do want it when you are doing jigsaw puzzle processing and maybe want to reassemble the pieces later).

convert start.png -crop 200x100+250+150 +repage z.png
identify z.png
z.png PNG 200x100 200x100+0+0 16-bit sRGB 51.1KB 0.000u 0:00.000

In your case, if you already cropped and saved without repaging, you can load your images and repage prior to actual use:

convert canvas-??.png +repage -crop 817x1040+0+0 ~/Pictures/odd/canvas.png

Upvotes: 6

Related Questions