合并一个图像而不是另一个图像时,RMagick透明度不起作用 [英] RMagick transparency not working when compositing one image over another
问题描述
在下面的代码中,我试图在一些山脉的图像上覆盖一个透明的方块。我认为它会起作用,但是通过设置background_color ='none'它不会使图像透明!
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')
这是我的输出图像:
编辑:我在Mac,Lion,ruby 1.9.3p125,ImageMagick 6.7.5-7
I'm on Mac, Lion, ruby 1.9.3p125, ImageMagick 6.7.5-7
编辑2:这在Heroku上运行正常!但不是在我的机器上。 Heroku具有相同版本的ImageMagick。奇怪:|
Edit 2: This works fine on Heroku! But not on my machine. Heroku has the same version of ImageMagick. Strange :|
推荐答案
出于某种原因 layer.alpha? == false
。所以我做了 sq.alpha(Magick :: ActivateAlphaChannel)
然后它工作了!
For some reason layer.alpha? == false
. So I did sq.alpha(Magick::ActivateAlphaChannel)
and then it worked!
参考: http://www.imagemagick.org/RMagick/doc/image1.html#alpha
这篇关于合并一个图像而不是另一个图像时,RMagick透明度不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!