Created
August 20, 2013 07:46
-
-
Save komasaru/6278280 to your computer and use it in GitHub Desktop.
Ruby script to compile colors in a image with RMagick.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'RMagick' | |
class RmagickCompileColor | |
# 画像ファイル | |
IMG_FILE = "img_1.jpg" | |
# この百分率未満は非表示 | |
RATE_MIN = 0.05 | |
def initialize | |
# アニメーション GIF が考慮されて画像は配列で読み込まれるので、 | |
# 配列の先頭画像を取得する。 | |
@img = Magick::Image.read(IMG_FILE).first | |
# 以下のように取得してもよい。 | |
#@img = Magick::ImageList.new(IMG_FILE) | |
# ピクセル数 | |
@px_x = @img.columns # 横 | |
@px_y = @img.rows # 縦 | |
@px_total = @px_x * @px_y # トータル | |
end | |
# 使用色集計 | |
def compile | |
begin | |
# 画像の Depth を取得 | |
img_depth = @img.depth | |
# カラーヒストグラムを取得してハッシュで集計 | |
hist = @img.color_histogram.inject({}) do |hash, key_val| | |
# 各ピクセルの色を16進で取得 | |
color = key_val[0].to_color(Magick::AllCompliance, false, img_depth, true) | |
# Hash に格納 | |
hash[color] ||= 0 | |
hash[color] += key_val[1] | |
hash | |
end | |
# ヒストブラムのハッシュを値の大きい順にソート | |
@hist = hist.sort{|a, b| b[1] <=> a[1]} | |
rescue => e | |
STDERR.puts "[ERROR][#{self.class.name}.compile] #{e}" | |
exit 1 | |
end | |
end | |
# 結果表示 | |
def display | |
begin | |
@hist.each do |color, count| | |
rate = (count / @px_total.to_f) * 100 | |
break if rate < RATE_MIN | |
puts "#{color} => #{count} px ( #{sprintf("%2.4f", rate)} % )" | |
end | |
puts | |
puts "Image Size: #{@px_x} px * #{@px_y} px" | |
puts "TOTAL : #{@px_total} px, #{@hist.size} colors" | |
rescue => e | |
STDERR.puts "[ERROR][#{self.class.name}.display] #{e}" | |
exit 1 | |
end | |
end | |
end | |
# 画像内使用色集計 | |
obj_main = RmagickCompileColor.new | |
obj_main.compile | |
obj_main.display |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment