LOGO

Find Large PNG Images with Linux Shell

December 20, 2007
Find Large PNG Images with Linux Shell

Identifying Oversized Images Using the Linux Command Line

When redesigning a website, careful consideration must be given to image dimensions within your article content. Specifically, determining which images exceed the boundaries of the new layout is crucial.

Having frequently used large screenshots in published articles, the need to identify images too wide for an increased sidebar became apparent. While creating a dedicated application would be feasible, the question arose: could this be accomplished directly through the Linux command line?

Leveraging the 'file' Command for Image Dimensions

Initial investigation revealed that the "file" command readily displays image size data for PNG files. For example:

$ file image3.png
image3.png: PNG image data, 613 x 657, 8-bit/color RGBA, non-interlaced

This functionality proved highly valuable, as the majority of images on the site are in PNG format.

Iterating Through Images with a Loop

To process all files within an upload directory, a simple loop was implemented:

$ 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---

While informative, this output required further processing, such as importing into a spreadsheet, to facilitate sorting.

Extracting Image Width with 'cut'

The Linux "cut" command was then utilized to isolate the width dimension. The command `cut -f5 -d\ ` extracts the fifth column, using a space as the delimiter. The backslash escapes the space, ensuring it's treated as a literal character.

$ for f in *.png;do file $f|cut -f5 -d\ ;done
631
631
375
484
---snipped---

Filtering Images Based on Width

To refine the output, a bash if statement was employed to display information only for images exceeding a specified width (600 pixels in this case). The backticks (`) around the `file $f | cut...` section execute the command and capture its output for comparison.

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

Outputting Filenames for Action

The final step involved modifying the command to output only the filenames of oversized images, enabling easy copying or relocation.

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

Conclusion

The Linux shell provides a remarkably powerful toolkit for automating tasks. While this solution may not be universally applicable, it demonstrates the potential of command-line utilities for addressing specific website maintenance needs. Understanding these capabilities can be invaluable for efficient workflow management.

#linux#png#images#shell#command line#pixel size