Ruby 如何修改图像的颜色以消除振动?
如何从以下内容更改颜色: 为此: 我使用Gimp生成输出图像,输入图像作为第一层,图像的背景色作为第二层,在层面板中我选择了模式“颜色” 我想保留背景色,但希望颜色是棕色Ruby 如何修改图像的颜色以消除振动?,ruby,image-processing,colors,imagemagick,chunkypng,Ruby,Image Processing,Colors,Imagemagick,Chunkypng,如何从以下内容更改颜色: 为此: 我使用Gimp生成输出图像,输入图像作为第一层,图像的背景色作为第二层,在层面板中我选择了模式“颜色” 我想保留背景色,但希望颜色是棕色 用ChunkyPNG做这个有什么想法吗?或者我应该在颜色查找表中使用ImageMagick吗?谢谢您的想法 我发现Linuxios的那个最有用 需要“json” 需要“httpclient” 需要“矮胖的png” 模块ChunkyPNG::Color def h(值) r、 g,b=r(值)。至f/MAX,g(值)。至f/
用ChunkyPNG做这个有什么想法吗?或者我应该在颜色查找表中使用ImageMagick吗?谢谢您的想法 我发现Linuxios的那个最有用
需要“json”
需要“httpclient”
需要“矮胖的png”
模块ChunkyPNG::Color
def h(值)
r、 g,b=r(值)。至f/MAX,g(值)。至f/MAX,b(值)。至f/MAX
最小值,最大值=[r,g,b]。最小值
如果max==min,则返回0
结果=最大情况
当r然后(g-b)/(max-min)+(g0
v=值
结束
ChunkyPNG::Color.hsv(色调、饱和度、v)
结束
image.save(filename.gsub(/\.png/,“\u brown.png”),:fast\u rgba)
结束
结束
结束
徽章=代码墙。徽章(“星象”)
上面的代码来自于网络上的一些片段
结果如下:相关:首先找出GIMP对“颜色”模式所做的像素数学运算,然后使用其中一个图像库进行像素数学运算。如果我是你,我会先对图像进行去饱和,然后将每个像素从灰度缩放到“对应”灰度通过一些缩放函数显示棕色。这里Ryan Bytes演示了如何执行此操作Ryan Bytes演示了如何在中执行非常类似的操作。
require "json"
require "httpclient"
require "chunky_png"
module ChunkyPNG::Color
def h(value)
r,g,b = r(value).to_f / MAX, g(value).to_f / MAX, b(value).to_f / MAX
min, max = [r,g,b].minmax
return 0 if max == min
result = case max
when r then (g - b) / (max - min) + (g < b ? 6 : 0)
when g then (b - r) / (max - min) + 2
when b then (r - g) / (max - min) + 4
end
result * 60
end
def s(value)
min, max = [r(value), g(value), b(value)].minmax.map { |value| value.to_f / MAX }
max == 0 ? 0 : (max - min) / max
end
def v(value)
[r(value), g(value), b(value)].max.to_f / MAX
end
def hsv(h, s, v)
h = h.to_f / 360
i = (h * 6).floor
f = h * 6 - i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
case i % 6
when 0 then r, g, b = v, t, p
when 1 then r, g, b = q, v, p
when 2 then r, g, b = p, v, t
when 3 then r, g, b = p, q, v
when 4 then r, g, b = t, p, v
when 5 then r, g, b = v, p, q
end
rgb *[r,g,b].map {|value| (value * 255).round }
end
end
module CoderWall
module_function
def badges(username)
url = URI.escape("https://coderwall.com/#{username}.json")
response = HTTPClient.get(url)
json = JSON.parse(response.body)
urls = json['badges'].map {|b| b['badge']}
brown = ChunkyPNG::Color.from_hex("#bf8a30")
hue = ChunkyPNG::Color.h(brown)
saturation = ChunkyPNG::Color.s(brown)
value = ChunkyPNG::Color.v(brown)
urls.each do |url|
matches = url.match /(\w+)\-/
response = HTTPClient.get(url)
filename = "./tmp/coderwall/"+matches[1]+".png"
File.open(filename,"w") {|f| f.write(response.body)}
image = ChunkyPNG::Image.from_file(filename)
image.pixels.map! do |pixel|
v = ChunkyPNG::Color.v(pixel)
unless ChunkyPNG::Color.a(pixel) > 0
v = value
end
ChunkyPNG::Color.hsv(hue,saturation, v)
end
image.save(filename.gsub(/\.png/,"_brown.png"), :fast_rgba)
end
end
end
badges = CoderWall.badges("astropanic")