0

The following code generates error.

#!/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

Any way to trim transparency using Wand?

Coder1979
  • 107
  • 9
  • 1
    Try using `rgba` in `wand_imageText.trim(color='rgba(0,0,0,0)',fuzz=0)`. Also use `rgba` in `draw.fill_color = 'rgba(255, 255, 255, 255)' ` That is the way transparent colors are defined in ImageMagick. Sorry I do not know Python Wand. But I presume it will use the same syntax as ImageMagick. See http://docs.wand-py.org/en/0.5.1/wand/color.html – fmw42 Mar 09 '19 at 19:23
  • Using rgba() doesn't work. It still generates the same error. – Coder1979 Mar 09 '19 at 19:51
  • Sorry, I do not know Wand. But have you posted your full code? I do not see the connection of the error message with what you have coded. Did you add `rgba` to all the places, including `background='rgba(0,0,0,0)'` – fmw42 Mar 09 '19 at 22:30
  • Yes, that's the full code. It's the trim function that's generating error. I have used rgba() everywhere. – Coder1979 Mar 10 '19 at 12:21
  • Does ImageMagick support trimming by the alpha value? – Coder1979 Mar 10 '19 at 12:22
  • `@Coder1979`.What do you mean by trimming by the alpha value? ImageMagick will automatically trim any transparency around the image, if that is what you mean. In fact it looks at each corner and determines how to trim each side. It does not need a color value provided to know what to trim. I am not sure what Imagick does with trim. – fmw42 Mar 10 '19 at 18:29
  • By 'trimming by the alpha value', I mean removing the outer area using the alpha value. For example, let's say the background color is rgba(0,0,0,0), the text color is rgba(0,0,0,255), and rgba(0,0,0,0) should be used for trimming. The text occupies the left-top pixel, so that automatic trim color selection cannot be used since that will remove the text, and the trim color must be specified. – Coder1979 Mar 11 '19 at 20:03
  • Looks like you found your own solution. – fmw42 Mar 11 '19 at 21:24

1 Answers1

3

I found out what I was doing wrong.

The color argument of the trim function must be a wand.color.Color object for the function to work.

The following code, that uses fmw42's suggestion of using rgba(), does trimming using the alpha value.

from wand.color import Color

wand_imageText.trim(color=Color('rgba(0,0,0,0)'),fuzz=0)
Coder1979
  • 107
  • 9