[IMAGE-SIG] ImageDraw hack with string centering function
E.M.L.
emlist@braznet.com.br
Mon, 22 Sep 1997 15:12:20 -0300
This is a multi-part message in MIME format.
--------------1CFBAE3959E2B60015FB7483
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
hello,
just a quick and small hack of the original ImageDraw.py
Since a lot of python imaging code newbie users might have experienced
the same problem, i have added a simple (but still to be improved)
centering function, which allows people (with less time to hack) to
draw one way centered strings to an image.
the idea of the function is quite simple: for comerciall uses of
image processing, some sort of alignment is quite always needed.
the hacking took as basis the original ImageDraw text function and
the Anthony Baxter enhImageDraw module.
if you need more than one-way alignment then better to use
Anthony Baxter enhImageDraw module.
Comments on the hack are welcome.
paulo b. bloedorn
Braznet Brazilian Network
--------------1CFBAE3959E2B60015FB7483
Content-Type: text/plain; charset=us-ascii; name="ImageDraw.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="ImageDraw.py"
#
# The Python Imaging Library
# $Id: ImageDraw.py,v 1.4 1996/11/10 17:52:14 fredrik Exp $
#
# drawing interface operations
#
# History:
# 96-04-13 fl Created (experimental)
# 96-08-07 fl Filled polygons, ellipses.
# 96-08-13 fl Added text support
#
# Copyright (c) Fredrik Lundh 1996. All rights reserved.
#
import Image
import math
class ImageDraw:
def __init__(self, im):
im.load()
self.im = im.im
self.ink = 255
self.fill = 0
self.font = None
def setink(self, ink):
self.ink = ink
def setfill(self, onoff):
self.fill = onoff
def setfont(self, font):
self.font = font
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 point(self, xy):
self.im.draw_point(xy, self.ink)
def polygon(self, xy):
self.im.draw_polygon(xy, self.ink, self.fill)
def rectangle(self, xy):
self.im.draw_rectangle(xy, self.ink, self.fill)
def ellipse(self, xy):
x1, y1, x2, y2 = xy
xc, yc = (x1 + x2) / 2, (y1 + y2) / 2
xr, yr = (x1 - x2) / 2, (y1 - y2) / 2
# FIXME: rough; will use the Path module instead
xy = []
for i in range(0, 360, 5):
a = i * math.pi / 180.0
xy.append(xc+xr*math.cos(a), yc+yr*math.sin(a))
self.im.draw_polygon(xy, self.ink, self.fill)
def text(self, xy, text):
import ImageFont
if not self.font:
# FIXME: add font repository
self.font = ImageFont.load_path("BDF/courR14.pil")
x, y = xy
m = self.font.getmask(text)
# FIXME: "paste ink" mechanism will be added
self.im.paste(Image.core.fill("L", m.size, self.ink),
(x, y, x + m.size[0], y + m.size[1]), m.im)
def centertext(self, (x,y), text, center=None):
# just a small hack of the original code above and the enhImageDraw code of Anthony Baxter
# is still quite handycapped but allows easy one-way centering.
# usage: ImageDraw.centertext((x,y),text,center)
# if center=x text will be horizontally centered
# case center=y text will be vertically centered
# set either x or y to zero if you want total centering
import ImageFont
if not self.font:
# FIXME: add font repository
self.font = ImageFont.load_path("BDF/courR14.pil")
m = self.font.getmask(text)
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 x == 0:
center == "x"
if y == 0:
center == "y"
if center == "x":
x = self.im.size[0]/2 - m.size[0]/2
if center == "y":
y = self.im.size[1]/2 - m.size[1]/2
# FIXME: "paste ink" mechanism will be added
self.im.paste(Image.core.fill("L", m.size, self.ink),
(x, y, x + m.size[0], y + m.size[1]), m.im)
--------------1CFBAE3959E2B60015FB7483--
_______________
IMAGE-SIG - SIG on Image Processing with Python
send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org
_______________