469,306 Members | 1,881 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Anaglyph 3D Stereo Imaging with PIL and numpy

Hi all!

i just found some colored glass and experimented a bit with
these red/green 3d images, so i thought i'd share this simple
script i wrote to generate such images with anyone whos
interested. i also wouldn't mind some comments on the code.

see ya, David.


*** anaglyph.py ***

# uses PIL from http://www.pythonware.com/
# and numpy from http://www.scipy.org/

from PIL import Image
import numpy

_magic = [0.299, 0.587, 0.114]
_zero = [0, 0, 0]
_ident = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]

# anaglyph methods from here:
# http://mitglied.lycos.de/stereo3d/an...comparison.htm

true_anaglyph = ([_magic, _zero, _zero], [_zero, _zero, _magic])
gray_anaglyph = ([_magic, _zero, _zero], [_zero, _magic, _magic])
color_anaglyph = ([_ident[0], _zero, _zero], [_zero, _ident[1], _ident[2]])
half_color_anaglyph = ([_magic, _zero, _zero], [_zero, _ident[1], _ident[2]])
optimized_anaglyph = ([[0, 0.7, 0.3], _zero, _zero], [_zero, _ident[1], _ident[2]])
methods = [true_anaglyph, gray_anaglyph, color_anaglyph, half_color_anaglyph, optimized_anaglyph]

def anaglyph(image1, image2, method=true_anaglyph):
m1, m2 = [numpy.array(m).transpose() for m in method]
im1, im2 = image_to_array(image1), image_to_array(image2)
composite = numpy.matrixmultiply(im1, m1) + numpy.matrixmultiply(im2, m2)
result = array_to_image(image1.mode, image1.size, composite)
return result

def image_to_array(im):
s = im.tostring()
dim = len(im.getbands())
return numpy.fromstring(s, numpy.UnsignedInt8).reshape(len(s)/dim, dim)

def array_to_image(mode, size, a):
return Image.fromstring(mode, size, a.reshape(len(a)*len(mode), 1).astype(numpy.UnsignedInt8).tostring())

if __name__=='__main__':
im1, im2 = Image.open("left-eye.jpg"), Image.open("right-eye.jpg")

anaglyph(im1, im2, half_color_anaglyph).save('output.jpg', quality=98)
May 3 '06 #1
1 3781
David Murmann wrote:
i just found some colored glass and experimented a bit with
these red/green 3d images, so i thought i'd share this simple
script i wrote to generate such images with anyone whos
interested. i also wouldn't mind some comments on the code.


Check out VPython -- there you can build 3-D models and
display them in real time while viewing with your glasses.
See:
http://www.vpython.org/FAQ.html

--Scott David Daniels
sc***********@acm.org
May 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.