[Image-SIG] Pixel-Graphics-Question. (Entertainment !)

Gregor Lingl glingl at aon.at
Wed Nov 10 01:33:04 CET 2004

Hi all of you!
(I'm a newcomer here)

Today the Daily Python Url led me to a program using pygame,
"Entertaining for at least a few minutes ": A Sinus Plasma


(As I'm a highschool teacher, I'm using entertainments like this also
to entertain my students. So I greatly appreciate to know about
ways to do pixel-graphics with Python  sufficently fast ..., you know:
to use it for Mandelbrot-, Julia-sets, Feigenbaum diagrams etc.)

I found it an easy exercise to write a version using Tkinter, i. e.
only the "batteries included" with Python:

from Tkinter import Tk, PhotoImage, Label
from math import sin, pi

WIDTH = 320     #width of screen
HEIGHT = 240    #height of screen

def main():
    root = Tk()
    pic = PhotoImage(width=WIDTH, height=HEIGHT)
    lbl = Label(root, image=pic)
    for x in range(WIDTH):
        for y in range(HEIGHT):
            z1 = sin(x/freq*1.7*pi)
            z2 = sin((x/3+y)/freq*1.5*pi)
            z3 = sin(y/freq*0.1*pi)

            z = abs(z1+z2+z3)*255
            color = "#%02x%02x%02x" % (int(z) % 256, int(z/4) % 256, 
int(z*4) % 256)
            pic.put(color, (x,y))
    root.update() # shift this stmt. 4 chars to the right to observe 
production of image

While this certainly is not state of the art, it works correctly,
but slower than the Pygame-version by a factor of nearly 10.
I'm not sure if this is because of the (implicit) use of the
Numeric-module by Pygame or because of the underlying graphics-
machinery ...

Now my question: is there an equally simple way to do it using PIL,
which is approximately equally fast as with Pygame. Or can it be
done (significantly) faster than the above version using Tkinter ... ?


Don't misunderstand me: nowadays teachers *have to* be *professional*
entertainers, which means, that for me entertaining is hard work.


More information about the Image-SIG mailing list