Bash: Fastest way of determining dimensions of image from URL

bashimagemagickwget

I'm trying to figure out a really fast method in bash of determining an images dimensions.

I know I could wget the image and then use imagemagick to determine the height and width of the image. I'm concerned that this may not be the fastest way of doing it.

I'm also concerned with having to install imagemagick when I only need a very small subset of functionality. I'm on an embedded system that has very limited resources (CPU, RAM, storage).

Any ideas?

Best Answer

As you note, you don't need the whole ImageMagick package. You just need identify.

You will also need the libraries the executable links to (and the libraries those libraries link to).

> whereis identify
identify: /bin/identify /usr/bin/identify /usr/share/man/man1/identify.1.gz
> ldd /bin/identify

ldd will show a list. When I did this, it included some X libs, libjpeg, etc. and two libraries clearly from the ImageMagick package, libMagickCore and libMagickWand. Those look to be linked to the same bunch of things, so if you have that, identify should work.

You don't have to download an entire image in order to get the dimensions, because these are in a header at the beginning of the file and that's what identify looks at. For example, here I'm copying the first 4 kB from a complete jpeg into a new file:

dd if=real.jpg of=test.jpg bs=1024 count=4

4 kB should be more than enough to include the header -- I'm sure you could do it with 1/4 that amount. Now:

>identify test.jpg 
test.jpg JPEG 893x558 893x558+0+0 8-bit DirectClass 4.1KB 0.000u 0:00.000

Those are the correct dimensions for real.jpg. Notice, however, that the size (4.1KB) is the size of the truncated file, since that information is not from the image header.

So: you only have to download the first kilobyte or so of each image.

Related Question