How-To Geek
Finding PNG Images Larger Than x Pixels Through the Linux Shell
When you are trying to work on changing the design of your website, you have to be concerned with the width of the pictures in your article content. I’ve got notoriously large screenshots on most of the articles I’ve written, so if I want to increase the sidebar it’s critical to figure out which pictures are going to be too wide to fit in the new design.
Since I’m a programmer, it would be easy for me to write a small application to do this, but it made me start thinking… why can’t I do this on the Linux command line?
The first thing I figured out was that PNG images display the size data when you run the “file” command on them:
$ file image3.png
image3.png: PNG image data, 613 x 657, 8-bit/color RGBA, non-interlaced
Very useful since 99% of the picture on this site are in PNG format. So now to throw it in a loop for all the files in my upload directory:
$ for f in *.png;do file $f;done
image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlaced
image1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlaced
image10.png: PNG image data, 375 x 395, 8-bit/color RGBA, non-interlaced
image11.png: PNG image data, 484 x 241, 8-bit/color RGBA, non-interlaced
—snipped—
This is more useful, but I’d have to pull the data into Excel or a similar application in order to sort the data, so I decided to use the linux “cut” command to pull out just the width column.
You’ll notice the -f5 parameter tells cut to take the fifth column, and the -d\ with a space after it tells cut to use a space as the delimiter. The slash \ character is an escape character to tell the shell to use the space as a character, and not as whitespace.
$ for f in *.png;do file $f|cut -f5 -d\ ;done
631
631
375
484
—snipped—
Not entirely useful output, is it? Let’s push that through a bash if statement, and then only show the output of the file command when the width is larger than 600 pixels.
Notice the ` (backtick) marks around the “file $f | cut…” section, which indicate that the commands inside the ` will be processed as a single output and fed into the if statement, where we use a -gt (greater than). Also note that you need spaces around either side of the brackets [ ]
for f in *.png;do if [ `file $f | cut -f5 -d\ ` -gt 600 ] ; then file $f;fi;done
image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlaced
image1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlaced
image17.png: PNG image data, 638 x 340, 8-bit/color RGBA, non-interlaced
image18.png: PNG image data, 608 x 448, 8-bit/color RGBA, non-interlaced
—snipped—
Now we have a list of all the files larger than 600 pixels wide. You could adjust the “file $f” at the end to just echo out the filenames if you needed to copy or move them somewhere else:
for f in *.png;do if [ `file $f | cut -f5 -d\ ` -gt 600 ] ; then echo $f;fi;done
image.png
image1.png
image17.png
image18.png
—snipped—
The Linux shell is incredibly powerful! This solution isn’t really practical for everybody, but it’s good to know how to work with the shell so you can accomplish this type of task when you need to.
|
Subscribe |
Daily Email Updates |
|
You can get our how-to articles in your inbox each day for free. Just enter your email below: |
- By The Geek on 12/20/07
Comments (9)
Comments are closed on this post.
If you'd like to continue the discussion on this topic, you can do so at our forum.
Go to the Forum

Damn those commands look complicated,
And then people wonder why linux is so unpopular with the *normal* people…
Just my 2 cents.
(linux user btw)
Victor,
I certainly wouldn’t advocate this type of thing for “normal” people =)
Thanks I needed that. I am trying to learn linux.
Any idea how to do this with jpg’s?
Dennis,
I’m not actually sure, but I think you’d have to find a utility that could check the dimensions of a jpg image, and you could substitute it on the command line.
Alternatively you could probably write a php, python or perl script and interface with the gd library.
wow…
You should put it into the convert command (imagemagick) to scale it if it’s bigger
Nice. This is a perfect example of the power of the Linux shell. It may look comlicated, but after you get over the learning curve, you can do things that are incredibly powerful.
Thanks for the example,
Daniel
daniel1992.wordpress.com
ImageMagick’s identify should be able to identify any files you have enabled while compiling it. For example,
identify ceaec3da6ef432af59fbdad2c93c277e.jpg
ceaec3da6ef432af59fbdad2c93c277e.jpg JPEG 160×90 160×90+0+0 DirectClass 8-bit 10.1797kb
Using ImageMagic’s identify, the command would be
for f in *.jpg;do if [ `file $f | cut -f3 -d\ | cut -f1 -dx` -gt 600 ] ; then echo $f;fi;done