root/stuff/sandbox/pil/makeillusion.py

Revision 393 (by effbot, 09/03/06 07:34:34)

added usage message

# inspired by
# http://www.johnsadowski.com/big_spanish_castle.php

import os, sys

from PIL import Image, ImageOps

try:
    filename = sys.argv[1]
except IndexError:
    sys.exit("usage: makeillusion.py <imagefile>")

basename = os.path.splitext(os.path.basename(filename))[0]

im = Image.open(sys.argv[1])

y, cr, cb = im.convert("YCbCr").split()

# luminance
y.save(basename + "-2.jpg")

# inverted color content
y = Image.new("L", y.size, 128)

im = Image.merge("YCbCr", (y, cr, cb)).convert("RGB")
im = ImageOps.autocontrast(ImageOps.invert(im))

x = im.size[0]/2
y = im.size[1]/2

# draw a dot to focus on
im.paste("black", (x-1, y-1, x+1, y+1))

im.save(basename + "-1.jpg")

print """
<img src='%s-1.jpg'
     onmouseover="this.src='%s-2.jpg';"
     onmouseout ="this.src='%s-1.jpg';"
     width='%d' height='%d' />
""" % (basename, basename, basename, im.size[0], im.size[1])
Note: See TracBrowser for help on using the browser.