Rmagick在水印中设置透明度的不透明度(Rmagick setting opacity in watermark with transparency)

编程入门 行业动态 更新时间:2024-10-10 09:25:39
Rmagick在水印中设置透明度的不透明度(Rmagick setting opacity in watermark with transparency)

我正在尝试创建具有不同不透明度值的水印(从0不透明值到1完全透明)。

我在ruby中有以下RMagick方法:

# 0 = opaque (Magick::OpaqueOpacity) 1= transparent (Magick::TransparentOpacity) def watermark(opacity = 0.99, size = 'm') manipulate! do |img| logo = Magick::Image.read("#{Rails.root}/app/assets/images/watermark#{size}.png").first logo.alpha(Magick::ActivateAlphaChannel) logo.opacity = (1 - opacity.to_f) * Magick::QuantumRange img.alpha(Magick::ActivateAlphaChannel) img = img.composite(logo, Magick::NorthWestGravity, 0, 0, Magick::OverCompositeOp) end end

我的问题是它似乎工作,但复合模式或alpha复合或设置不透明度或alpha失败,因为我在图像中得到黑色透明度。 例如,如果我的水印是带有文字的完全透明的图像,我将其放在汽车图像上,那么我会得到一个带有水印的更暗或夜间图像,因此水印的背景不能正确混合。

有没有正确设置水印图像中不透明度的建议? 也许有一些解决水印的方法?

编辑:添加图像示例:

http://uppix.com/f-watermarkg53925b100016ab8e.png (水印) http://oi62.tinypic.com/2us8rxl.jpg (基本图片) http://oi60.tinypic.com/2pt6mg3.jpg (作文)

I am trying to create a watermark with different opacity values (from 0 opaque value to 1 totally transparent).

I have the following method for RMagick in ruby:

# 0 = opaque (Magick::OpaqueOpacity) 1= transparent (Magick::TransparentOpacity) def watermark(opacity = 0.99, size = 'm') manipulate! do |img| logo = Magick::Image.read("#{Rails.root}/app/assets/images/watermark#{size}.png").first logo.alpha(Magick::ActivateAlphaChannel) logo.opacity = (1 - opacity.to_f) * Magick::QuantumRange img.alpha(Magick::ActivateAlphaChannel) img = img.composite(logo, Magick::NorthWestGravity, 0, 0, Magick::OverCompositeOp) end end

My problem is that it seems to work, but the composite mode or the alpha composite or setting the opacity or alpha is failing, because I get a black transparency in the image. For example if my watermark is a totally transparent image with a text, that I put over a car image, then I get a more dark or nightly image with the watermark, so the background of the watermark it is not blending properly.

Any suggestions to set properly the opacity in the watermark image? Maybe some method to disolve the watermark?

EDIT: Adding image examples:

http://uppix.com/f-watermarkg53925b100016ab8e.png (watermark) http://oi62.tinypic.com/2us8rxl.jpg (base image) http://oi60.tinypic.com/2pt6mg3.jpg (composition)

最满意答案

感谢Neil Slater,我终于找到了正确的解决方案。 我需要在最终结果中结合使用 DstIn + Over的复合操作 :

def watermark(opacity = 0.99, size = 'm') manipulate! do |img| logo = Magick::Image.read("#{Rails.root}/app/assets/images/watermark#{size}.png").first logo.alpha(Magick::ActivateAlphaChannel) white_canvas = Magick::Image.new(logo.columns, logo.rows) { self.background_color = "none" } white_canvas.alpha(Magick::ActivateAlphaChannel) white_canvas.opacity = Magick::QuantumRange - (Magick::QuantumRange * opacity) # Important: DstIn composite operation (white canvas + watermark) logo_opacity = logo.composite(white_canvas, Magick::NorthWestGravity, 0, 0, Magick::DstInCompositeOp) logo_opacity.alpha(Magick::ActivateAlphaChannel) # Important: Over composite operation (original image + white canvas watermarked) img = img.composite(logo_opacity, Magick::NorthWestGravity, 0, 0, Magick::OverCompositeOp) end end

Thanks to Neil Slater, I finally found the right solution. I need a combination of composite operation of DstIn + Over in my finalt result:

def watermark(opacity = 0.99, size = 'm') manipulate! do |img| logo = Magick::Image.read("#{Rails.root}/app/assets/images/watermark#{size}.png").first logo.alpha(Magick::ActivateAlphaChannel) white_canvas = Magick::Image.new(logo.columns, logo.rows) { self.background_color = "none" } white_canvas.alpha(Magick::ActivateAlphaChannel) white_canvas.opacity = Magick::QuantumRange - (Magick::QuantumRange * opacity) # Important: DstIn composite operation (white canvas + watermark) logo_opacity = logo.composite(white_canvas, Magick::NorthWestGravity, 0, 0, Magick::DstInCompositeOp) logo_opacity.alpha(Magick::ActivateAlphaChannel) # Important: Over composite operation (original image + white canvas watermarked) img = img.composite(logo_opacity, Magick::NorthWestGravity, 0, 0, Magick::OverCompositeOp) end end

更多推荐

watermark,水印,opacity,image,图像,电脑培训,计算机培训,IT培训"/> <meta name="

本文发布于:2023-04-29 09:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:明度   水印   透明度   不透   Rmagick

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!