Converting ImageMagick commands to RMagick

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
RomainT
Posts: 1
Joined: 2012-05-24T00:00:30-07:00
Authentication code: 13

Converting ImageMagick commands to RMagick

Post by RomainT »

Hi!

I realized an image processing with ImageMagick commands and I would like to port them to RMagick. The goal of this task is to take a picture and to pixelate given areas (one or more) for privacy purpose.

Here are my bash script (script.sh) which work very well using convert command:

Code: Select all

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
I have the original picture, the pixelated picture, the mask and I get the result.

Now, I want to create the Ruby version of this script using ImageMagick. Here is what I have now:

Code: Select all

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 (everythink else must be transparent).
  # Then I have to superpose the resulting image to the original.
end
As you can see, I'm stuck. I don't know how to extract the part of pixelated that overlap with the white part of the mask.
Which RMagick method could I use?

Thanks! :)
Post Reply