Note that there are some explanatory texts on larger screens.

plurals
  1. POPort ImageMagick commands to RMagick
    text
    copied!<p>I'm doing 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.</p> <p>Here is my bash script (<code>script.sh</code>), which works very well using the <code>convert</code> command:</p> <pre class="lang-sh prettyprint-override"><code>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 </code></pre> <p>Now I want to create the Ruby version of this script using ImageMagick. Here is what I have now:</p> <pre><code>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 </code></pre> <p>As you can see, I'm stuck at the last step. What operation do I need to do with <a href="http://i.imgur.com/IWtBF.png" rel="nofollow">my original picture</a>, <a href="http://i.imgur.com/ZJIgt.png" rel="nofollow">my pixelated picture</a> and <a href="http://i.imgur.com/Mup1j.png" rel="nofollow">my mask</a> in order to have <a href="http://i.imgur.com/e0qzA.png" rel="nofollow">this result</a>?</p> <p>How can I build an image with just the overlapping of the white part of the mask and the pixelated picture. Just like <a href="http://i.imgur.com/KVC8a.png" rel="nofollow">this one</a> but with transparency instead of black?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload