【发布时间】:2012-05-24 12:20:42
【问题描述】:
我正在使用 ImageMagick 命令进行图像处理,我想将它们移植到 RMagick。此任务的目标是拍照并像素化给定区域(一个或多个)以保护隐私。
这是我的 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
如您所见,我被困在最后一步。我需要对my original picture、my pixelated picture和my mask进行什么操作才能拥有this result?
我怎样才能构建一个只有蒙版的白色部分和像素化图片重叠的图像。就像this one 一样,但是透明而不是黑色?
【问题讨论】:
-
没有错误。我只是不知道如何处理我的原始图像、图像的像素化版本和蒙版。我在帖子中添加了图片。
标签: ruby imagemagick rmagick