有没有办法使用 Wand(ImageMagick Python 绑定)trim 透明区域?
Is there a way to trim transparent areas using Wand (the ImageMagick Python binding)?
以下代码会产生错误。
#!/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()
File "C:\Program Files\Python37\lib\site-packages\wand\image.py", line 865, in wrapped
result = function(self, *args, **kwargs) File "C:\Program Files\Python37\lib\site-packages\wand\image.py", line 4444, in trim
with color or self[0, 0] as color: AttributeError: enter
有什么方法可以使用 Wand trim 透明度吗?
我发现我做错了什么。
trim 函数的颜色参数必须是 wand.color.Color 对象,函数才能工作。
以下代码使用了 fmw42 关于使用 rgba() 的建议,trim使用 alpha 值。
from wand.color import Color
wand_imageText.trim(color=Color('rgba(0,0,0,0)'),fuzz=0)
以下代码会产生错误。
#!/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()
File "C:\Program Files\Python37\lib\site-packages\wand\image.py", line 865, in wrapped result = function(self, *args, **kwargs) File "C:\Program Files\Python37\lib\site-packages\wand\image.py", line 4444, in trim with color or self[0, 0] as color: AttributeError: enter
有什么方法可以使用 Wand trim 透明度吗?
我发现我做错了什么。
trim 函数的颜色参数必须是 wand.color.Color 对象,函数才能工作。
以下代码使用了 fmw42 关于使用 rgba() 的建议,trim使用 alpha 值。
from wand.color import Color
wand_imageText.trim(color=Color('rgba(0,0,0,0)'),fuzz=0)