[IMAGE-SIG] SETINK - font colors on stamped strings

Anthony Baxter Anthony Baxter <arb@connect.com.au>
Mon, 22 Sep 1997 18:41:28 +1000

>>> Daniel Michelson wrote
> The functionality you're looking for is undocumented. So, the answer to
> your question is not obvious. You should be able to define your own
> palette and then draw your text using setink method which looks up a
> purely red color in the palette. PIL palettes are binary strings with
> 768 members with the format rgbrgbrgb...

I posted an ImagePalette module some time ago to the list - have a look in
the archives for it. As far as some of the other options, such as centering
a string, I have a module called 'enhImageDraw' which does some of this.

Some of the code isn't all that pretty, but it works for me :)

Here's the useful bits from it for text...

# enhanced ImageDraw functionality. Depends on new ImagePalette code.

import Image,ImageDraw,ImageFont,ImagePalette

class enhImageDraw(ImageDraw.ImageDraw):
    "enhanced version of the PIL ImageDraw class"

    def text(self,(x,y),text,halign=None,valign=None,rot=None):
	"""enhanced text method. obeys colours, x and y can be -ve, center
	   keyword arg (x/y/both)"""
	if rot == 180:
	if rot == 270:
	if rot == 90:

	if x < 0:
	    x = self.im.size[0] + x - m.size[0]
	if y < 0:
	    y = self.im.size[1] + y - m.size[1]

	if halign == "center":
	    x = x - m.size[0]/2
	if halign == "right":
	    x = x - m.size[0]

	if valign == "center":
	    y = y - m.size[1]/2
	if valign == "bottom":
	    y = y - m.size[1]

		(x, y, x + m.size[0], y + m.size[1]), m.im)

    def line(self, xy, xy1 = None):
        if xy1 == None:
            self.im.draw_lines(xy, self.ink)
            self.im.draw_line(xy, xy1, self.ink)

    def shadowtext(self,(x,y),text,mcol,scol,halign=None,valign=None):
	"""draw text with a 'shadow'. Front text will be in colour mcol, 
           shadow in colour scol."""

    def _newfilled(self,(x,y)):
	return im

IMAGE-SIG - SIG on Image Processing with Python

send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org