[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)"""
m=self.font.getmask(text)
if rot == 180:
m=m.transpose(Image.ROTATE_180)
if rot == 270:
m=m.transpose(Image.ROTATE_270)
if rot == 90:
m=m.transpose(Image.ROTATE_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]
self.im.paste(self._newfilled(m.size).im,
(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)
else:
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."""
self.setink(scol)
self.text((x-1,y+1),text,halign,valign)
self.setink(mcol)
self.text((x,y),text,halign,valign)
def _newfilled(self,(x,y)):
im=Image.new("P",(x,y))
im.im.draw_rectangle((0,0,x,y),self.ink,1)
return im
_______________
IMAGE-SIG - SIG on Image Processing with Python
send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org
_______________