ImageMagick convert to scale jpgs + failing to crop correctly?

This post was written by eli on June 21, 2019
Posted Under: Linux,Software

Instead of using my scanner, I put my cell phone on some kind of stand, and shot a lot of paper documents (voice activation is a blessing). But then the files are unnecessarily large. Don’t need all that resolution. So

$ for i in * ; do convert "$i" -scale '33%' -quality 75 "smaller/scan_$i" ; done

And the files are 100-200k each with enough resolution to see the fine print.

Crop fail

This drove me craze for an hour: I wanted to crop the image, so that only the paper part would be left. Selecting the region in GIMP and then using it as in

$ convert in.jpg -crop 2632x3712+152+960 -scale '33%' -quality 75 out.jpg

Failed miserably, giving me an image if wrong dimensions and showing the wrong place, even though I correctly picked width x height + offsetx + offsety, with reference to the topleft corner. Exactly the output that GIMP displayed when I selected the desired area.

The problem turned out to be that that the cell phone wrote the orientation data into the JPG file (as it should). So the coordinates showed in GIMP were after rotating the image, and had therefore nothing to do with those given as the -crop argument.

Solution: In order to obtain the reference image to obtain coordinates with GIMP, go

$ convert in.jpg -strip for-gimp.jpg

And then use those coordinates for cropping. I suppose adding -strip to the conversion command is a good idea as well, to prevent further confusion, in particular if a -rotate is added to get the rotation correctly:

$ convert in.jpg -strip -crop 3712x2632+952+200 -scale '33%' -quality 75 out.jpg

The reason this is so confusing is that ImageMagick retained the orientation information, so both the input and output images were displayed in the same orientation, even though the coordinated used by the -crop flag related to the unrotated image.

Add a Comment

required, use real name
required, will not be published
optional, your blog address