RMagick transparency not working when compositing one image over another

北战南征 提交于 2019-12-22 09:17:56

问题


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:

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 :|


回答1:


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



来源:https://stackoverflow.com/questions/12284637/rmagick-transparency-not-working-when-compositing-one-image-over-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!