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

给你一囗甜甜゛ 提交于 2019-12-11 20:37:41

问题


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

回答1:


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.




回答2:


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/



来源:https://stackoverflow.com/questions/35489257/how-do-i-get-the-hex-value-from-each-pixel-in-rmagick-or-chunky-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!