ImageMagick调整大小-设置横向和纵向图像的宽度

问题描述:

我正在尝试将图像调整为给定宽度,并按比例调整高度-无论图像是横向还是纵向.

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.

我有两张图像,我已经用阅读的内容来测试了ImageMagick convert -resize命令,我希望以下内容适用于任何类型的图像:

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:

  • 源图像1:landscape3264 × 2448,调整为200 × 150 ==> WORKS
  • 源图像2:portrait3240 × 4320,其大小调整为 ==> 失败
  • Source Image 1: landscape, 3264 × 2448, resizes to 200 × 150 ==> WORKS
  • Source Image 2: portrait, 3240 × 4320, resizes to 150 × 200 ==> FAIL

现在,我知道我可以通过以下方式解决此问题:预先读取源图像的​​尺寸并调整命令(例如,使用x200进行人像显示似乎将宽度正确设置为200),但是我不禁认为必须是让ImageMagick处理此问题的方法.

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.

我尝试了以下变体,但得到了相同的不正确结果:

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

如JWK的答案评论中所述:

As discussed in the comments of the answer by JWK:

图像似乎是横向的,而不是纵向的.某些显示图像的应用程序会从图像的exif配置文件中读取其他信息,以确定方向.这引起了一些混乱,因为ImageMagick不会自动使用exif配置文件中的信息.可以使用自动定向"选项强制执行此操作.该命令应更改为此:

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

200x也可以写为200,但是如果应该调整宽度或高度的大小,则使用200x或x200会更好.

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