New  Edit  Freeze  Diff  Attatch
Top  List  Search  Last Update  Backup  Help 



easy_install PIL

Install on mac

Mac has problem with jpeg and truetype2 library

  1. Download jpeg and truetype2 package
  2. For each package, do
    1. ./configure
    2. make
    3. sudo make install
  3. Then, download PIL (do this again if you setup above already)
    2. sudo python install

True transparent image and paste it on the background image

import Image,ImageDraw,ImageFont,ImageChops

# Create a text image
width = 217 
height = 140 

# 1. Create a base image
baseim ="base.png")

# 2. Create a transparent text image
im ="RGB", (120, 210), (0,0,0))
alpha ="L", im.size, "black")
# Make a grayscale image of the font, white on black.
imtext ="L", im.size, 0)
drtext = ImageDraw.Draw(imtext)
font = ImageFont.truetype("Tahoma.ttf", 30) 
drtext.text((0,0), "helloworld", font=font, fill="white")

# Add the white text to our collected alpha channel. Gray pixels around
# the edge of the text will eventually become partially transparent
# pixels in the alpha channel.
alpha = ImageChops.lighter(alpha, imtext)

# Make a solid color, and add it to the color layer on every pixel
# that has even a little bit of alpha showing.
solidcolor ="RGBA", im.size, "#000000")
immask = Image.eval(imtext, lambda p: 255 * (int(p != 0)))
im = Image.composite(solidcolor, im, immask)
im.putalpha(alpha)"transtext.png", "PNG") #save for testing

# paste this on the base image with the alpha info


Captcha Example

import Image,ImageDraw,ImageFont

# Create a background image
image ='RGB', (300, 50), (220,210,190))
draw = ImageDraw.Draw(image)

# Create a text image
textImg ='RGB',(150,40),(0,0,0))
tmpDraw = ImageDraw.Draw(textImg)
textFont = ImageFont.truetype('ARIAL.TTF',36)
tmpDraw.text((0, 0), 'Hello', font = textFont, fill = (10,200,200))
textImg = textImg.rotate(-10)

# Create a mask (same size as the text image)
mask ='L',(150, 40),0) 

# Paste text image with the mask


import os,sys
import Image

def resize(filename):
   size = 48,48
   im =


Simple watermarking

import Image

baseim ="original.png")
logoim ="logo.png") #transparent image
baseim.paste(logoim,(baseim.size[0]-logoim.size[0],baseim.size[1]-logoim.size[1]),logoim) #specify the 3rd argument to add alpha'new.png','PNG')


Reload   New Edit Diff Attatch   Top List Search Last Update Backup   Help   RSS
"PukiWiki" 1.4.3 Copyright © 2001,2002,2003 PukiWiki Developers Team. License is GNU/GPL.
Based on "PukiWiki" 1.3 by sng Powered by PHP 5.4.45