Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-03-2008, 01:52 PM   #1
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Python: Can you add text to an image in python?

Can I use python to put text on an image, like "Property of me" or the file name something of the sort? What about multiple bits of text, like "proeprty of" AND the file name in separate corners of the image?

I know I can do it with imagemagick, I'm just wondering if I can do it directly inside of python (without calling an external program).
Old 04-03-2008, 03:48 PM   #2
Senior Member
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
The Python Imaging Library should be able to do such...
Old 04-03-2008, 03:56 PM   #3
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Originally Posted by ranger_nemo View Post
The Python Imaging Library should be able to do such...

If anyone else is interested, here's my quick example (complete with transparency):
import Image, ImageDraw, ImageFont
from os import chdir, path

def txt2img(text,bg="#ffffff",fg="#000000",font="Verdana.ttf",FontSize=14):
    font_dir = "/usr/share/fonts/truetype/msttcorefonts/"
    img_name = "sweet.jpg"
    font_size = FontSize
    fnt = ImageFont.truetype(font_dir+font, font_size)
    lineWidth = 20
    img ="ayo20.jpg")
    imgbg ='RGBA', img.size, "#000000") # make an entirely black image
    mask ='L',img.size,"#000000")       # make a mask that masks out all
    draw = ImageDraw.Draw(img)                     # setup to draw on the main image
    drawmask = ImageDraw.Draw(mask)                # setup to draw on the mask
    drawmask.line((0, lineWidth/2, img.size[0],lineWidth/2),
                  fill="#999999", width=10)        # draw a line on the mask to allow some bg through
    img.paste(imgbg, mask=mask)                    # put the (somewhat) transparent bg on the main
    draw.text((10,0), text, font=fnt, fill=bg)      # add some text to the main
    del draw,"JPEG",quality=100)  

txt2img("This is a really weird image")



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 07:02 PM
LXer: Charming Python: Python elegance and warts, Part 2 LXer Syndicated Linux News 0 08-31-2007 08:40 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 11:05 AM
Python Add Image Gerardoj Programming 2 11-09-2003 04:24 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration