Can ImageMagick return the image size?

北城余情 提交于 2020-01-11 17:16:30

问题


I'm using ImageMagick from the command line to resize images:

convert -size 320x240 image.jpg

However, I don't know how to determine the size of the final image. Since this is a proportional image scale, it's very possible that new image is 100x240 or 320x90 in size (not 320x240).

Can I call the 'convert' command to resize the image and return the new image dimensions? For example, pseudo code:

convert -size 320x240 -return_new_image_dimension image.jpg   // returns the new resized image dimensions

回答1:


You could use an extra call to identify:

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



回答2:


-ping option

This option is also recommended as it prevents the entire image from being loaded to memory, as mentioned at: https://stackoverflow.com/a/22393926/895245 :

identify -ping -format '%w %h' image.jpg

Tested on ImageMagick 6.7.7, Ubuntu 14.04.

See also: Fast way to get image dimensions (not filesize)




回答3:


I'm not sure with the %w and %h format. While Photoshop says my picture is 2678x3318 (and I really trust Photoshop), identify gives me:

identify -ping -format '=> %w %h' image.jpg
=> 643x796

(so does [fx:w] and [fx:h])

I had to use

identify -ping -format '=> %[width] %[height]' image.jpg
=> 2678x3318

I don't know what's going on here, but you can see both values on standard output (where the width and height before the => are the correct ones)

identify -ping image.jpg
image.jpg PAM 2678x3318=>643x796 643x796+0+0 16-bit ColorSeparation CMYK 2.047MB 0.000u 0:00.000

The documentation says %w is the current width and %[width] is original width. Confusing.

%w and %h may be correct for most uses, but not for every picture.




回答4:


If you specify option -verbose, convert prints:

original.jpg=>scaled.jpg JPEG 800x600=>100x75 100x75+0+0 8-bit sRGB 4.12KB 0.020u 0:00.009
                                       ^^^^^^


来源:https://stackoverflow.com/questions/1555509/can-imagemagick-return-the-image-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!