RMagickでモザイク処理をしてみた。

像。
モザイク処理をしてみた。
例えばモザイクの単位を5ピクセルにする場合
縦5ピクセル、横5ピクセルの5*5マスのRGB値平均値を算出。
該当範囲をその色で塗りつぶす。

require 'rubygems'
require "RMagick"

include Magick

def mozaic(image, filename, per)

img = ImageList.new(image)

px = (img.columns / per).ceil
py = (img.rows / per).ceil

for y in 0...py+1
  for x in 0...px+1
    tx = x * per
    ty = y * per
    r_array = Array.new
    g_array = Array.new
    b_array = Array.new
    for iy in 0...per
      for ix in 0...per
        pixel = img.pixel_color(tx+ix, ty+iy)
        r_array << pixel.red
        g_array << pixel.green
        b_array << pixel.blue
      end
    end
    mr = r_array.inject(0){|r,i| r+=i }/r_array.size
    mg = g_array.inject(0){|r,i| r+=i }/g_array.size
    mb = b_array.inject(0){|r,i| r+=i }/b_array.size
    color = Magick::Pixel.new(mr, mg, mb)

    for iy in 0...per
      for ix in 0...per
        img.pixel_color(tx+ix, ty+iy, color)
      end
    end
  end
end

img.write filename
end

image = ARGV[0]
filename = ARGV[1]
per = ARGV[2]
mozaic(image, filename, per.to_i)

本日の実験画像は……おや…?

おやおや…?

これは…まさか…?



ホリーさんだった!