无法保留透明度使用ImageMagick的复合

问题描述:

我有两个图像(两个PNG)与透明度。我使用MiniMagick宝石将两张单张图像复制到另外两张图像中。然后,我想要将这些图像中的一个图像合成在一起,从而保持透明度。无法保留透明度使用ImageMagick的复合

使用下面的代码,它是尊重image2的透明度,但是一旦它放置在image1的顶部(这就是我所追求的),image1的透明度变为黑色!我需要保留透明度,但我真的不确定如何正确使用alpha透明度,如果这是适当的工具。

image = MiniMagick::Image.open("skin.png") 
image1 = MiniMagick::Image.open(image.path) 
image2 = MiniMagick::Image.open(image.path) 

# Crop and scale image1 
MiniMagick::Tool::Mogrify.new do |m| 
    m.crop '8x8+8+8' 
    m.scale '144x144' 
    m.background 'transparent' 
    m.extent '160x160-8-8' 
    m << image1.path 
end 

# Crop and scale image2 
MiniMagick::Tool::Mogrify.new do |m| 
    m.crop '8x8+40+8' 
    m.scale '160x160' 
    m << image2.path 
end 

result = image1.composite(image2) do |c| 
    c.compose 'Over' 
    c.alpha 'On' 
end 

result.write "public/skins/#{profile}.png" 

send_file "public/skins/#{profile}.png" 

谢谢。

MiniMagick的组合处理为带有初始值的jpg。

http://www.rubydoc.info/github/probablycorey/mini_magick/MiniMagick/Image#composite-instance_method

下面的代码为我工作。

result = image1.composite(image2, 'png') do |c| 
    c.channel "A" 
    c.alpha 'Activate' 
    c.compose 'Over' 
end