How do I get the hex value from each pixel in RMagick or Chunky_PNG?

1.6k views Asked by At

I'm trying to read the value of each pixel as hex (HTML notation) using RMagick or Chunky_PNG e.g. #5DBCD2. At the moment I have the below which kind of does what I want but I couldn't find the right way to actually read the hex value. I'd prefer to use Chunky_PNG though, thanks!

require 'chunky_png'

img = ChunkyPNG::Image.from_file("image.png")

height = img.dimension.height
width  = img.dimension.width

height.times do |i|
  width.times do |j|
    p [ChunkyPNG::Color.r(img[j,i]), ChunkyPNG::Color.g(img[j,i]), ChunkyPNG::Color.b(img[j,i])]
  end
end

OR

require 'RMagick'
include Magick

image = ImageList.new("image.png")
(0..image.columns).each do |x|
  (0..image.rows).each do |y|
    pixel = image.pixel_color(x, y)
    p [pixel.red, pixel.green, pixel.blue]
  end
end
2

There are 2 answers

0
Nabeel On BEST ANSWER

A bit of a hacky solution but it should do the trick.

require 'chunky_png'

img = ChunkyPNG::Image.from_file("image.png")

height = img.dimension.height
width  = img.dimension.width

height.times do |i|
  width.times do |j|
    arr = [ChunkyPNG::Color.r(img[j,i]), ChunkyPNG::Color.g(img[j,i]), ChunkyPNG::Color.b(img[j,i])]
    p "\##{arr.map {|x| x.to_s(16).rjust(2, '0')}.join.upcase}"
  end
end

Say for example you want the value of [204, 102, 0] (which translates to #cc6600) you could use the following code.

"\##{[204, 102, 0].map {|x| x.to_s(16).rjust(2, '0')}.join.upcase}"
=> "#CC6600"

To break it down .to_s(16) converts an integer to hexadecimal format and .rjust(2, '0') prepends '0' if the string does not match 2 characters in length.

Hope this helps.

0
Stephen Grimes On

If using RMagick, you can use the to_s method on each pixel color to find the corresponding hex value. It's not totally straightforward, but this blog post explains how to go about it:

http://blog.kellishaver.com/image-color-analysis-with-rmagick/