There are a few ways to enlarge a PNG image larger than x pixels using the Linux shell. The first way is to use the command “png -i”. This will create a new PNG file with the size specified by the argument. The second way is to use “pnm -f”. This will force the PNG file to be created with a format that allows for larger images. The third way is to use “png -L”. This will list all of the files in a directory and show you which ones have an image size of x pixels or more.


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:

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:

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.

image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlacedimage1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlacedimage10.png: PNG image data, 375 x 395, 8-bit/color RGBA, non-interlacedimage11.png: PNG image data, 484 x 241, 8-bit/color RGBA, non-interlaced—snipped—

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.

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.

631631375484—snipped—

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 [ ]

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:

image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlacedimage1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlacedimage17.png: PNG image data, 638 x 340, 8-bit/color RGBA, non-interlacedimage18.png: PNG image data, 608 x 448, 8-bit/color RGBA, non-interlaced—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.

image.pngimage1.pngimage17.pngimage18.png—snipped—