Check a directory for transparent pngs recursively

Tagged with Ruby shell ImageMagick

Language: Ruby

This requires that you have ImageMagick installed on whatever machine you run it on as the `identify -format %A file.png` bit is actually a shell command for ImageMagick. Ruby just was easier for me to figure out how to recursively run that on all .png files in a given directory. I’m sure there is a shorter shell script to do the same thing. The script will probably run without ImageMagick installed, but it most likely won’t give any output.

View as text

require 'find'

def find_and_print(path, pattern)
  Find.find(path) do |entry|
    if File.file?(entry) and entry[pattern]
      has_transparency = `identify -format %A #{entry}`
      if has_transparency.to_s.chomp.strip == "True"
        puts "#{entry}"
      end
    end
  end
end
 
# print all the ruby files
find_and_print("/rails_app/public/images", /.+\.png$/)
Original snippet written by Jon Kinney
Last updated at 10:02 AM on Feb 02, 2010

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.