How to draw text antialiased on alpha channel only

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ramaglabs
Posts: 3
Joined: 2015-07-21T05:51:31-07:00
Authentication code: 1151

How to draw text antialiased on alpha channel only

Post by ramaglabs »

We are upgrading from an old version of ImageMagick and finding that a command we have previously used now produces different results.

In ImageMagick 6.7.3-9 the following produces greyscale text on a transparent background, where the text is a solid colour (#AAAAAA) and is antialiased against the transparent background on the alpha channel only:

Code: Select all

convert -background transparent -resize 1024x768 -fill '#AAAAAAB8' -font Arial -pointsize 50 label:'Test Stamp' test.png
In ImageMagick 6.9.1-2 the same command produces greyscale text on a transparent background where the text is antialiased on the RGB channels (i.e. pixels are shades of grey). It appears that the pixel colour '#AAAAAA' gets scaled by the alpha value '#B8', so that the brightest pixel becomes '7A'.

ImageMagick 6.7.3-9:
Image

ImageMagick 6.9.1-2:
Image

How do we get ImageMagick 6.9.1-2 to produce the same results as the older version?
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: How to draw text antialiased on alpha channel only

Post by 246246 »

ramaglabs wrote: How do we get ImageMagick 6.9.1-2 to produce the same results as the older version?
I am not sure but is it what you want?

Code: Select all

convert -background transparent -resize 1024x768 -fill '#AAAAAAB8' -font Arial -pointsize 50 label:'Test Stamp' PNG8:test.png
identify -verbose test.png
...
Alpha: graya(0,0) #00000000
Colors: 2
Histogram:
197335: ( 0, 0, 0, 0) #00000000 graya(0,0)
40233: (170,170,170,255) #AAAAAAFF graya(170,1)
...
ramaglabs
Posts: 3
Joined: 2015-07-21T05:51:31-07:00
Authentication code: 1151

Re: How to draw text antialiased on alpha channel only

Post by ramaglabs »

Thanks, but not quite. That produces text that's the correct colour (#AAAAAA) but has a hard outline and is not semi transparent. What I am looking for is the pixels to be '#AAAAAAB8' except around the glyph edges where they should blend with the transparent background, so have varying alpha values, e.g. for the old ImageMagick we get:

Code: Select all

$ identify -verbose test.png
  Alpha: graya(0,0,0,0)   #00000000
  Colors: 200
  Histogram:
    158362: (  0,  0,  0,  0) #00000000 graya(0,0,0,0)
     13398: (170,170,170,  0) #AAAAAA00 graya(170,170,170,0)
     12851: (170,170,170,184) #AAAAAAB8 graya(170,170,170,0.721569)
      3891: (170,170,170,185) #AAAAAAB9 graya(170,170,170,0.72549)
      2297: (170,170,170,186) #AAAAAABA graya(170,170,170,0.729412)
      1693: (170,170,170,187) #AAAAAABB graya(170,170,170,0.733333)
      1484: (170,170,170,  1) #AAAAAA01 graya(170,170,170,0.00392157)
      1370: (170,170,170,188) #AAAAAABC graya(170,170,170,0.737255)
      1310: (170,170,170,190) #AAAAAABE graya(170,170,170,0.745098)
...
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to draw text antialiased on alpha channel only

Post by glennrp »

Looks like the old sRGB versus linear problem is contributing to this.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to draw text antialiased on alpha channel only

Post by fmw42 »

It is a bug in IM 6.9.1.5 onward. See viewtopic.php?f=3&t=28081. Similarly, it is a bug in 6.9.1.2 and 6.9.1.3 versions. It seems to be fine before 6.9.1.2 and in 6.9.1.4.
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: How to draw text antialiased on alpha channel only

Post by 246246 »

glennrp wrote:Looks like the old sRGB versus linear problem is contributing to this.
Ah, so if the text is black the problem doesn't occur (checked with 6.9.1-3 on Cygwin).

So workaround may be:

Code: Select all

convert -background transparent  -resize 1024x768 -fill '#000000B8' -font Arial -pointsize 50 label:'Test Stamp' -alpha off -fill '#AAAAAA' -opaque black -alpha on test.png
It looks similar result is generated but I don't know how antialias exactly works.
(On Cygwin, Arial not found, so I omit -font for this test. Also I'm not sure if this works on 6.9.1-2.)

Alpha: graya(170,0) #AAAAAA00
Colors: 196
Histogram:
133270: (170,170,170, 0) #AAAAAA00 graya(170,0)
10808: (170,170,170,184) #AAAAAAB8 graya(170,0.721569)
4141: (170,170,170,185) #AAAAAAB9 graya(170,0.72549)
2974: (170,170,170,186) #AAAAAABA graya(170,0.729412)
1965: (170,170,170,187) #AAAAAABB graya(170,0.733333)
1565: (170,170,170,183) #AAAAAAB7 graya(170,0.717647)
1537: (170,170,170, 1) #AAAAAA01 graya(170,0.00392157)

But the current 6.9.1-9 release on Windows, it does not work. As Fred said, it seems there stays another problem.

[EDIT]
With 6.9.1-9 on Windows

Code: Select all

convert -background transparent  -resize 1024x768 -fill '#000000B8' -font Arial -pointsize 50 label:'Test Stamp' -type 'TrueColorAlpha' -alpha off -fill '#AAAAAA' -opaque black -alpha on test.png
seems to work as well.

Alpha: graya(170,0) #AAAAAA00
Colors: 198
Histogram:
168052: (170,170,170, 0) #AAAAAA00 graya(170,0)
10458: (170,170,170,184) #AAAAAAB8 graya(170,0.721569)
4593: (170,170,170,185) #AAAAAAB9 graya(170,0.72549)
2812: (170,170,170,186) #AAAAAABA graya(170,0.729412)
1752: (170,170,170,187) #AAAAAABB graya(170,0.733333)
1532: (170,170,170,188) #AAAAAABC graya(170,0.737255)
1288: (170,170,170,183) #AAAAAAB7 graya(170,0.717647)
ramaglabs
Posts: 3
Joined: 2015-07-21T05:51:31-07:00
Authentication code: 1151

Re: How to draw text antialiased on alpha channel only

Post by ramaglabs »

246246 wrote:
glennrp wrote:Looks like the old sRGB versus linear problem is contributing to this.
With 6.9.1-9 on Windows

Code: Select all

convert -background transparent  -resize 1024x768 -fill '#000000B8' -font Arial -pointsize 50 label:'Test Stamp' -type 'TrueColorAlpha' -alpha off -fill '#AAAAAA' -opaque black -alpha on test.png
seems to work as well.

Alpha: graya(170,0) #AAAAAA00
Colors: 198
Histogram:
168052: (170,170,170, 0) #AAAAAA00 graya(170,0)
10458: (170,170,170,184) #AAAAAAB8 graya(170,0.721569)
4593: (170,170,170,185) #AAAAAAB9 graya(170,0.72549)
2812: (170,170,170,186) #AAAAAABA graya(170,0.729412)
1752: (170,170,170,187) #AAAAAABB graya(170,0.733333)
1532: (170,170,170,188) #AAAAAABC graya(170,0.737255)
1288: (170,170,170,183) #AAAAAAB7 graya(170,0.717647)
Thanks - I can confirm the above does work when tested in ImageMagick 6.9.1-2. We found a few other places in our app where we generate text via IM that also has similar problems, so rather than change them all to use this work around, we switched to using ImageMagick 6.9.1-4. As fmw42 points out that version doesn't have this bug. This works for us for now, and hopefully when we next update IM that bug will be resolved.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: How to draw text antialiased on alpha channel only

Post by dlemstra »

ImageMagick 6.9.1-10 has been released this weekend.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply