ImageMagick可以返回图像大小吗?

Jac*_*obT 33 unix command-line image imagemagick image-processing

我从命令行使用ImageMagick来调整图像大小:

convert -size 320x240 image.jpg
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何确定最终图像的大小.由于这是比例图像比例,因此新图像很可能是100x240或320x90(不是320x240).

我可以调用'convert'命令来调整图像大小并返回新的图像尺寸吗?例如,伪代码:

convert -size 320x240 -return_new_image_dimension image.jpg   // returns the new resized image dimensions
Run Code Online (Sandbox Code Playgroud)

Arj*_*jan 43

您可以使用额外的电话identify:

convert -size 320x240 image.jpg; identify -format "%[fx:w]x%[fx:h]" image.jpg

  • 好奇,为什么`识别-format"%[fx:w] x%[fx:h]"`更喜欢`识别-format"%[w] x%[h]"`? (3认同)

Cir*_*四事件 35

-ping还建议使用选项,因为它可以防止将整个映像加载到内存中,如下所述:https://stackoverflow.com/a/22393926/895245:

identify -ping -format '%w %h' image.jpg
Run Code Online (Sandbox Code Playgroud)

在ImageMagick 6.7.7,Ubuntu 14.04上测试.

另请参见:获取图像尺寸的快速方式(不是文件大小)


小智 6

我不确定%w%h格式。虽然 Photoshop 说我的图片是 2678x3318(我真的很信任 Photoshop),但identify给了我:

identify -ping -format '=> %w %h' image.jpg
=> 643x796
Run Code Online (Sandbox Code Playgroud)

([fx:w] 和 [fx:h] 也是如此)

我不得不使用

identify -ping -format '=> %[width] %[height]' image.jpg
=> 2678x3318
Run Code Online (Sandbox Code Playgroud)

我不知道这里发生了什么,但是您可以在标准输出中看到这两个值(=>之前的宽度和高度是正确的)

identify -ping image.jpg
image.jpg PAM 2678x3318=>643x796 643x796+0+0 16-bit ColorSeparation CMYK 2.047MB 0.000u 0:00.000
Run Code Online (Sandbox Code Playgroud)

文档说 %w 是当前宽度, %[width] 是原始宽度。令人困惑。

%w并且%h可能适用于大多数用途,但不适用于每张图片。