In comp.lang.python, Duncan Booth <> (Duncan Booth)
wrote in <Xns93C2B04C1DD91duncanrcpcouk@127.0.0.1>::
|Marc Wilson <> wrote in
|news::
|
|> Oh, and- is there a way to overwrite text onto an image? The site is
|> a house-sales site, and we want to overwrite "SOLD" across the
|> thumbnail once a property is sold. It looks like I can do this with
|> the ImageDraw module, but I can't see how to replicate what we do now
|> with Image Robot, which is to write "SOLD" across the image diagonally
|> (using the Add Watermark feature). Any ideas?
|
|How about this:
|
|from PIL import Image, ImageFont, ImageDraw, ImageChops
|
|im = Image.open("test.jpg")
|im.thumbnail((128, 12

, Image.ANTIALIAS)
|
|font = ImageFont.truetype("arial.ttf", 30)
|
|def AddOverlay(im, origin, text, angle=-45):
| # Create an overlay with white text and subtract it from the image.
| # This effectively blacks out the area to be overlaid.
| overlay = Image.new(im.mode, im.size)
| draw = ImageDraw.Draw(overlay)
| draw.text(origin, text, (255, 255, 255), font=font)
| overlay = overlay.rotate(angle)
| stamped = ImageChops.subtract(im, overlay, 1, 0)
|
|
| # Now create a red overlay and add it to the subtracted image
| overlay = Image.new(im.mode, im.size)
| draw = ImageDraw.Draw(overlay)
| draw.text(origin, text, (255, 0, 0), font=font)
| overlay = overlay.rotate(angle)
| stamped = ImageChops.add(stamped, overlay, 1, 0)
| return stamped
|
|stamped = AddOverlay(im, (10, 50), "SOLD!")
|stamped.show()
Yep- that works a treat. Incidentally, why do I need to remove the text
area before overwriting it? Or am I missing the point?
--
Marc Wilson
Cleopatra Consultants Limited - IT Consultants
2 The Grange, Cricklade Street, Old Town, Swindon SN1 3HG
Tel: (44/0) 70-500-15051 Fax: (44/0) 870 164-0054
Mail:
Web:
http://www.cleopatra.co.uk
__________________________________________________ _______________
Try MailTraq at
https://my.mailtraq.com/register.asp?code=cleopatra