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
A bit of a hacky solution but it should do the trick.
Say for example you want the value of [204, 102, 0] (which translates to
#cc6600
) you could use the following code.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.