如何从RMagick或Chunky_PNG中的每个像素获取十六进制值? [英] How do I get the hex value from each pixel in RMagick or Chunky_PNG?

查看:297
本文介绍了如何从RMagick或Chunky_PNG中的每个像素获取十六进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 RMagick Chunky_PNG 来读取每个像素的值,如十六进制(HTML表示法)例如#5DBCD2 。目前我有下面哪种做我想要的,但我找不到正确的方式来实际读取十六进制值。我宁愿使用 Chunky_PNG ,谢谢!

  require' chunky_png'

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

height = img.dimension.height
width = img.dimension。宽度

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
解决方案

$ b

解决方案

一些hacky解决方案,但它应该做的伎俩。

  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
结束

例如,您想要[204,102,0]的值(转换为#cc6600 )你可以使用下面的代码。

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

分解 .to_s(16)如果字符串不匹配2个字符,code>将整数转换为十六进制格式, .rjust(2,'0') prepends'0'。 / p>

希望这有助于。


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.

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.

这篇关于如何从RMagick或Chunky_PNG中的每个像素获取十六进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆