foobar
foobar

Reputation: 11384

RMagick transparency not working when compositing one image over another

In the following code I'm trying to overlay a transparent square over the image of some mountains. I thought it would work, but by setting background_color = 'none' it doesn't make the image transparent!

The result is a black square over the top left corner - desired result is the black square should be transparent.

require 'open-uri'
require 'RMagick'

image_url = 'http://farm9.staticflickr.com/8446/7937080514_62d7749860.jpg'

bg = Magick::ImageList.new
open(image_url, 'rb') do |f|
  bg.from_blob(f.read)
end

layer = Magick::Image.new(200, 200) {
  self.background_color = 'none'
}

bg.each do |frame|
  frame.composite!(layer, 0, 0, Magick::OverCompositeOp)
  frame.strip!
end

bg.write('out.jpg')

Here's my output image:

output with none transparent black square

Edit: I'm on Mac, Lion, ruby 1.9.3p125, ImageMagick 6.7.5-7

Edit 2: This works fine on Heroku! But not on my machine. Heroku has the same version of ImageMagick. Strange :|

Upvotes: 4

Views: 2334

Answers (1)

foobar
foobar

Reputation: 11384

For some reason layer.alpha? == false. So I did sq.alpha(Magick::ActivateAlphaChannel) and then it worked!

Reference: http://www.imagemagick.org/RMagick/doc/image1.html#alpha

Upvotes: 3

Related Questions