Posted: . At: 10:35 AM. This was 2 years ago. Post ID: 16331
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to check image dimensions using the command-line on Linux.


It is very easy to check image dimensions using the command line. Install the exiv2 package and this allows easy retrieval of image information.

Just like this.

┌──(john㉿DESKTOP-PF01IEE)-[/mnt/c/Users/Intel i5/Pictures/phone5]
└─$ exiv2 ui_icon_equipment.png 
File name       : ui_icon_equipment.png
File size       : 8626195 Bytes
MIME type       : image/png
Image size      : 4096 x 4096
ui_icon_equipment.png: No Exif data found in the file

This gives us a lot of information about the file. The dimensions are what we are after.

The example below tests to see if an image is over 1000 pixels wide.

┌──(john㉿DESKTOP-PF01IEE)-[/mnt/c/Users/Intel i5/Pictures/phone5]
└─$ exiv2 ui_icon_equipment.png 2>/dev/null | awk ' NR==4 { if($4 > 1000) print $4 " x " $6 " Pixels"; else print "No adequately sized image found." }'
4096 x 4096 Pixels

This is easily done using the awk if statements.

if($4 > 1000)
    print $4 " x " $6 " Pixels";
else
    print "No adequately sized image found."

The awk utility is so versatile and makes this type of task very simple.

┌──(john㉿DESKTOP-PF01IEE)-[/mnt/c/Users/Intel i5/Pictures/phone5]
└─$ exiv2 ui_icon_equipment.png 2>/dev/null | awk ' NR==2 { print $4 " Bytes"; print sum ($4 / 1024 / 1024) " Megabytes" }' 
8626195 Bytes
8.22658 Megabytes

The above example will print the file size of an image in bytes and then megabytes. This is also very useful.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.