ImageMagick resize - set width for both landscape

2019-05-01 03:33发布

问题:

I'm trying to get images resized to a given width, with the height being adjusted proportionally - no matter if the image has landscape or portrait orientation.

I have two images I test the ImageMagick convert -resize command with and from what I have read, I would expect the following to work for any kind of image:

convert source.jpg -resize 200 out.jpg

The problem is, it doesn't. Here are my results:

  • Source Image 1: landscape, 3264 × 2448, resizes to 200 × 150 ==> WORKS
  • Source Image 2: portrait, 3240 × 4320, resizes to 150 × 200 ==> FAIL

Now, I know I could fix this by reading in the source image dimensions beforehand and making adjustments to the command (e.g. using x200 for portrait seems to set the width correctly to 200) but I can't help but think that there must be a way to let ImageMagick handle this.

I read the documentation and googled for answers but cannot seem to solve this. Any help is greatly appreciated.

EDIT:

I have tried the following variation but get the same incorrect result:

convert source.jpg -resize 200x out.jpg

FIX:

convert source.jpg -auto-orient -resize 200 out.jpg

回答1:

As discussed in the comments of the answer by JWK:

The images seem to be landscape instead of portrait. Some applications that show an image read extra information from the exif profile of the image to determine the orientation. This was causing some confusion because ImageMagick does not automatically use the information from the exif profile. This can be forced with the auto-orient option. The command should be changed to this:

convert source.jpg -auto-orient -resize 200x out.jpg

The 200x can also be written as 200 but using 200x or x200 shows better if the width or the height should be resized.



回答2:

I got this solved. Turned out that the image information of my portrait source file was somehow messed up.

I ran the identify portrait_source.jpg command and noticed that it had the width and height wrong ( 4320 x 3240 instead of 3240 × 4320). So basically, ImageMagick thought it was looking at a landscape oriented file when really it was a portrait oriented file. All other programs (OSX Finder, Preview) showed the image in portrait orientation.

Simply opening the image in Preview and saving it fixed the image information and after that the resize worked as expected.

So, if you run into an issue similar to mine, make sure to check the image information on your source image. I have no idea why the information was wrong in the first place. I used a photo made with a digital camera.