【问题标题】:Is there a way to trim transparent areas using Wand (the ImageMagick Python binding)?有没有办法使用 Wand(ImageMagick Python 绑定)修剪透明区域?
【发布时间】:2019-03-09 18:09:19
【问题描述】:

以下代码会产生错误。

#!/usr/bin/env python

import collections.abc
from wand.image import Image, COMPOSITE_OPERATORS, DISTORTION_METHODS, CHANNELS
from wand.drawing import Drawing

wand_imageText = Image(width=1080,
                   height=1080,
                   background='rgb(0,0,0,0)')

with Drawing() as draw:        
    draw.font = 'Impact'
    draw.font_size = 100
    draw.gravity = 'north_west'
    draw.fill_color = 'rgb(255, 255, 255, 255)' 
    draw.text(0, 0, "Let's rock!")        
    draw(wand_imageText)

wand_imageText.trim(color='rgb(0,0,0,0)',fuzz=0)

wand_imageText.save(filename='C:\\Temp\\Wand_trim_test.jpg')
wand_imageText.close()

文件“C:\Program Files\Python37\lib\site-packages\wand\image.py”,第 865 行,已包装 结果 = 函数(self,*args,**kwargs)文件“C:\Program Files\Python37\lib\site-packages\wand\image.py”,第 4444 行,修剪 以颜色或 self[0, 0] 作为颜色:AttributeError: enter

有什么方法可以使用 Wand 修剪透明度?

【问题讨论】:

  • 尝试在wand_imageText.trim(color='rgba(0,0,0,0)',fuzz=0) 中使用rgba。在draw.fill_color = 'rgba(255, 255, 255, 255)' 中也使用rgba,这就是ImageMagick 中定义透明颜色的方式。对不起,我不知道 Python Wand。但我认为它将使用与 ImageMagick 相同的语法。见docs.wand-py.org/en/0.5.1/wand/color.html
  • 使用 rgba() 不起作用。它仍然会产生同样的错误。
  • 对不起,我不知道魔杖。但是您发布了完整的代码吗?我没有看到错误消息与您编码的内容之间的联系。你有没有把rgba添加到所有地方,包括background='rgba(0,0,0,0)'
  • 是的,这就是完整的代码。这是产生错误的修剪功能。我到处都用过 rgba()。
  • ImageMagick 是否支持按 alpha 值修剪?

标签: python imagemagick wand


【解决方案1】:

我发现我做错了什么。

修剪函数的颜色参数必须是 wand.color.Color 对象才能使函数工作。

以下代码使用 fmw42 建议使用 rgba(),使用 alpha 值进行修剪。

from wand.color import Color

wand_imageText.trim(color=Color('rgba(0,0,0,0)'),fuzz=0)

【讨论】:

    猜你喜欢
    • 2011-12-31
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2016-01-07
    相关资源
    最近更新 更多