LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 04-03-2008, 02:52 PM   #1
BrianK
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, 04:48 PM   #2
ranger_nemo
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...

http://www.pythonware.com/products/pil/
 
Old 04-03-2008, 04:56 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

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

http://www.pythonware.com/products/pil/
indeed.

If anyone else is interested, here's my quick example (complete with transparency):
Code:
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 = Image.open("ayo20.jpg")
    imgbg = Image.new('RGBA', img.size, "#000000") # make an entirely black image
    mask = Image.new('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 
    img.save(img_name,"JPEG",quality=100)  



txt2img("This is a really weird image")
before:
http://hero.com/~briank/misc/ex/ayo20.jpg

after:
http://hero.com/~briank/misc/ex/sweet.jpg
 
  


Reply


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 08:02 PM
LXer: Charming Python: Python elegance and warts, Part 2 LXer Syndicated Linux News 0 08-31-2007 09:40 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 08:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 12:05 PM
Python Add Image Gerardoj Programming 2 11-09-2003 05:24 PM


All times are GMT -5. The time now is 05:06 PM.

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