【问题标题】:Unable to retain transparency using ImageMagick's composite使用 ImageMagick 的合成无法保持透明度
【发布时间】:2014-11-03 01:24:32
【问题描述】:

我有两张透明的图片(均为 png)。我正在使用MiniMagick gem 将单个图像的两个副本裁剪成另外两个图像。然后,我想将这些图像中的一个叠加在一起,一直保持透明度。

使用下面的代码,它尊重 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"

谢谢。

【问题讨论】:

    标签: imagemagick transparency minimagick


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2011-05-06
      • 1970-01-01
      • 2015-10-29
      • 2017-10-18
      • 2014-07-01
      相关资源
      最近更新 更多