Can ImageMagick return the image size?
Asked Answered
T

5

51

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
Tormoria answered 12/10, 2009 at 16:21 Comment(0)
F
54

You could use an extra call to identify:

convert -size 320x240 image.jpg; identify -format "%[fx:w]x%[fx:h]" image.jpg
Fandango answered 12/10, 2009 at 16:29 Comment(2)
curious, why is identify -format "%[fx:w]x%[fx:h]" preferable to identify -format "%[w]x%[h]" ?Florafloral
I think it is not, @mark. I cannot imagine that back in 2009 the %w and %h were not supported yet. But if fx: has some side effect, then I guess I would have mentioned that as well. Weird. I will do some testing...Fandango
A
62

-ping option

This option is also recommended as it prevents the entire image from being loaded to memory, as mentioned at: https://mcmap.net/q/161826/-get-image-dimensions-using-batch-script-imagemagick:

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

man identify says:

-ping                efficiently determine image attributes

We can for example test it out with some of the humongous images present on Wikimedia's "Large image" category e.g. this ultra high resolution image of Van Gogh's Starry Night which Wikimedia claims is 29,696 × 29,696 pixels, file size: 175.67 MB:

wget -O image.jpg https://upload.wikimedia.org/wikipedia/commons/e/e8/Van_Gogh_-_Starry_Night_-_Google_Art_Project-x0-y0.jpg
time identify -ping -format '%w %h' image.jpg
time identify       -format '%w %h' image.jpg

I however observed that -ping at least in this case did not make any difference on the time, maybe it only matters for other image formats?

Tested on ImageMagick 6.9.10, Ubuntu 20.04.

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

Agnusago answered 28/9, 2015 at 13:50 Comment(0)
F
54

You could use an extra call to identify:

convert -size 320x240 image.jpg; identify -format "%[fx:w]x%[fx:h]" image.jpg
Fandango answered 12/10, 2009 at 16:29 Comment(2)
curious, why is identify -format "%[fx:w]x%[fx:h]" preferable to identify -format "%[w]x%[h]" ?Florafloral
I think it is not, @mark. I cannot imagine that back in 2009 the %w and %h were not supported yet. But if fx: has some side effect, then I guess I would have mentioned that as well. Weird. I will do some testing...Fandango
H
8

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.

Halda answered 11/1, 2017 at 8:13 Comment(0)
V
1

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
                                       ^^^^^^
Viyella answered 16/11, 2016 at 8:34 Comment(1)
I want to read the width of the magick file using magick and then resize it such that height always remains 900pixel. I tried to use magick resize identify -format '%wx2' urban7.jpg but it fails. I am on Windows system so if any batch commands are there , i could use some. Please help. I came across this but could not understand it https://stackoverflow.com/questions/56672830/how-to-resize-an-image-in-imagemagick-but-keep-an-aspect-ratio-constant/56673304#56673304Inkstand
U
0

You can do that in Imagemagick as follows:

convert input.jpg -resize 320x240 -write output.jpg -format "%wx%h\n" info:
Unscientific answered 10/2, 2024 at 5:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.