ImageMagickコマンドを使用して画像処理を行っていますが、それらをRMagickに移植したいと思います。このタスクの目標は、写真を撮り、プライバシーの目的で特定の領域(1つ以上)をピクセル化することです。
これが私のbashスクリプト( )で、コマンドscript.sh
を使用すると非常にうまく機能します。convert
convert invoice.png -scale 10% -scale 1000% pixelated.png
convert invoice.png -gamma 0 -fill white -draw "rectangle 35, 110, 215, 250" mask.png
convert invoice.png pixelated.png mask.png -composite result.png
次に、ImageMagickを使用してこのスクリプトのRubyバージョンを作成します。これが私が今持っているものです:
require 'rmagick'
# pixelate_areas('invoice.png', [ [ x1, y1, width, height ] ])
def pixelate_areas(image_path, areas)
image = Magick::Image::read(image_path).first
pixelated = image.scale(0.1).scale(10)
mask = Magick::Image.new(image.columns, image.rows) { self.background_color = '#000' }
areas.each do |coordinates|
area = Magick::Image.new(coordinates[2], coordinates[3]) { self.background_color = '#fff' }
mask.composite!(area, coordinates[0], coordinates[1], Magick::OverCompositeOp)
end
# Now, how can I merge my 3 images?
# I need to extract the part of pixelated that overlap with the white part of the mask (everything else must be transparent).
# Then I have to superpose the resulting image to the original (it's the easy part).
end
ご覧のとおり、私は最後のステップで立ち往生しています。この結果を得るには、元の画像、ピクセル化された画像、およびマスクに対してどのような操作を行う必要がありますか?
マスクの白い部分とピクセル化された画像を重ね合わせただけで画像を作成するにはどうすればよいですか。これと同じですが、黒ではなく透明度がありますか?