anti-aliased text with wx.GraphicsContext?

I've never used PIL don't have it on my computer, and have put enough
trouble into the AA font thing for some days and so for now I think
I'll give this a rest. But if I get curious and want to take another
shot at getting smoother fonts, I might try that at some point.
Thanks.

Che

···

On Wed, Sep 1, 2010 at 12:35 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:

Robin Dunn wrote:

On 8/31/10 4:27 PM, C M wrote:

Is there any other processing that I could apply through Cairo to the
text to produce a more printed text-like effect?

Not that I know of but you can assume that the rendering being done with
the win32 APIs (wx.DC or wx.GraphicsContext and apparently Cairo) has been
optimized for use on the screen with a balance between rendering speed and
AA quality, and also that it can possibly take into account the
characteristics of the display such as the ordering of the RGB sub-elements
of each pixel on an LCD.

All true, however, there is much debate about whether MS has done a good job
with all of that or not. In general comparing, for instance, MS to Apple,
folks seem to say that Apple's approach is "prettier", and MS's perhaps
easier to read on the screen.

See this discussion:

http://www.antigrain.com/research/font_rasterization/index.html

trying to do it yourself from Python would certainly not be worth the
effort.

True, though if there are any functional freetype bindings that might be an
option.

Easier, however, would be to see if PIL does what you want -- there is even
an (optional) AGG renderer for PIL. It's pretty easy to move PIL images to
wx for display -- see the wxPython Wiki for examples.

C M wrote:

PIL_text_test.py (427 Bytes)

···

<Chris.Barker@noaa.gov> wrote:

Easier, however, would be to see if PIL does what you want -- there is even
an (optional) AGG renderer for PIL. It's pretty easy to move PIL images to
wx for display -- see the wxPython Wiki for examples.

I've never used PIL don't have it on my computer, and have put enough
trouble into the AA font thing for some days and so for now I think
I'll give this a rest. But if I get curious and want to take another
shot at getting smoother fonts, I might try that at some point.
Thanks.

Well, my own curiosity got to me, so here is an example, rendered by PIL (and the script that generated it). You can decide it it does any better. This is using the built-in PIL rendering, which uses freetype for text.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thanks for the code/image. Yes, it looks like what The GIMP produces;
as Robin said, I guess GIMP is using freetype. I think it is a bit of
an improvement, other than the "W".

I didn't think I'd touch this, but when I saw how short that code was
and that there was a Windows installer for PIL...within about a minute
I was able to try it out. Unfortunately it hit this snag on my
computer:

Traceback (most recent call last):
  File "PIL_text_test.py", line 15, in <module>
    font = ImageFont.truetype("Vera.ttf", 60)
  File "C:\Python25\Lib\site-packages\PIL\ImageFont.py", line 227, in truetype
    return FreeTypeFont(filename, size, index, encoding)
  File "C:\Python25\Lib\site-packages\PIL\ImageFont.py", line 134, in __init__
    self.font = core.getfont(file, size, index, encoding)
IOError: cannot open resource

Any idea what the problem is?

Thanks,
Che

···

On Wed, Sep 1, 2010 at 2:02 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:

C M wrote:

<Chris.Barker@noaa.gov> wrote:

Easier, however, would be to see if PIL does what you want -- there is
even
an (optional) AGG renderer for PIL. It's pretty easy to move PIL images
to
wx for display -- see the wxPython Wiki for examples.

I've never used PIL don't have it on my computer, and have put enough
trouble into the AA font thing for some days and so for now I think
I'll give this a rest. But if I get curious and want to take another
shot at getting smoother fonts, I might try that at some point.
Thanks.

Well, my own curiosity got to me, so here is an example, rendered by PIL
(and the script that generated it). You can decide it it does any better.
This is using the built-in PIL rendering, which uses freetype for text.

-Chris

On Wed, Sep 1, 2010 at 2:02 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:

C M wrote:

<Chris.Barker@noaa.gov> wrote:

Easier, however, would be to see if PIL does what you want -- there is
even
an (optional) AGG renderer for PIL. It's pretty easy to move PIL images
to
wx for display -- see the wxPython Wiki for examples.

I've never used PIL don't have it on my computer, and have put enough
trouble into the AA font thing for some days and so for now I think
I'll give this a rest. But if I get curious and want to take another
shot at getting smoother fonts, I might try that at some point.
Thanks.

Well, my own curiosity got to me, so here is an example, rendered by PIL
(and the script that generated it). You can decide it it does any better.
This is using the built-in PIL rendering, which uses freetype for text.

C M wrote:

I didn't think I'd touch this, but when I saw how short that code was
and that there was a Windows installer for PIL...within about a minute
I was able to try it out. Unfortunately it hit this snag on my
computer:

Traceback (most recent call last):
  File "PIL_text_test.py", line 15, in <module>
    font = ImageFont.truetype("Vera.ttf", 60)
  File "C:\Python25\Lib\site-packages\PIL\ImageFont.py", line 227, in truetype
    return FreeTypeFont(filename, size, index, encoding)
  File "C:\Python25\Lib\site-packages\PIL\ImageFont.py", line 134, in __init__
    self.font = core.getfont(file, size, index, encoding)
IOError: cannot open resource

Any idea what the problem is?

yup -- you need to point PIL to a truetype file (*.ttf). when I ran the code, I had Vera.ttf in my working directory, just to make it easy. Change "Vera.ttf" to a path to a valid ttf file, and you should be all set.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thanks, Chris, works now.

The final step: writing the words to a wxPanel?

-Che

···

On Wed, Sep 1, 2010 at 4:35 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:

C M wrote:

I didn't think I'd touch this, but when I saw how short that code was
and that there was a Windows installer for PIL...within about a minute
I was able to try it out. Unfortunately it hit this snag on my
computer:

Traceback (most recent call last):
File "PIL_text_test.py", line 15, in <module>
font = ImageFont.truetype("Vera.ttf", 60)
File "C:\Python25\Lib\site-packages\PIL\ImageFont.py", line 227, in
truetype
return FreeTypeFont(filename, size, index, encoding)
File "C:\Python25\Lib\site-packages\PIL\ImageFont.py", line 134, in
__init__
self.font = core.getfont(file, size, index, encoding)
IOError: cannot open resource

Any idea what the problem is?

yup -- you need to point PIL to a truetype file (*.ttf). when I ran the
code, I had Vera.ttf in my working directory, just to make it easy. Change
"Vera.ttf" to a path to a valid ttf file, and you should be all set.

The final step: writing the words to a wxPanel?

(Unless you meant taking that image and displaying the words as a
bitmap in the panel...that's probably what you were going for. I
haven't touched PIL at all before this, so I don't have a sense for
what is possible in conjunction with wxPython)

C M wrote:

The final step: writing the words to a wxPanel?

(Unless you meant taking that image and displaying the words as a
bitmap in the panel...that's probably what you were going for.

yup -- that's what you need to do. See this Wiki page for how:

http://wiki.wxpython.org/WorkingWithImages

(there are other relevant pages, too)

If you want to combine text with other drawing then you need to either do all the drawing with PIL, or you can draw with wx to a bitmap, move it to PIL, draw text there, move it back, etc -- not exactly high performing, but it should work.

You also may be able to draw the PIL-drawn text bitmap(with a transparent background) on top of a wxBitmap, but I'm not sure if the Alpha blending will be done right in that case.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thanks, Chris. I may give this a try at some point.

Che

···

On Wed, Sep 1, 2010 at 7:22 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:

C M wrote:

The final step: writing the words to a wxPanel?

(Unless you meant taking that image and displaying the words as a
bitmap in the panel...that's probably what you were going for.

yup -- that's what you need to do. See this Wiki page for how:

http://wiki.wxpython.org/WorkingWithImages

(there are other relevant pages, too)

If you want to combine text with other drawing then you need to either do
all the drawing with PIL, or you can draw with wx to a bitmap, move it to
PIL, draw text there, move it back, etc -- not exactly high performing, but
it should work.

You also may be able to draw the PIL-drawn text bitmap(with a transparent
background) on top of a wxBitmap, but I'm not sure if the Alpha blending
will be done right in that case.

-Chris