Search code examples
ruby-on-railsrubypngtransparencyalpha-transparency

Ruby: Find non-transparent PNG and convert into JPG


What is the smartest way to find out if a PNG has transparency or not in Ruby? Is it OK if I just check if there's an alpha-channel? If yes: How do I check for an alpha-channel?

I'm writing a script that will convert all PNGs without transparency into smaller JPGs.

Thanks a lot for your help!

UPDATE: In the meantime I've written that script and you can find it at Github.


Solution

  • Checking the alpha channel seems the most sensible thing to do. Is it safe to assume you are using RMagick? did you read the documentation?

    >> require 'RMagick'
    >> image = Magick::Image.read("a.png").first
    >> image.alpha?
    => true
    

    http://www.imagemagick.org/RMagick/doc/image1.html#alpha_q