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).
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
andlibMagickWand
. 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:4 kB should be more than enough to include the header -- I'm sure you could do it with 1/4 that amount. Now:
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.