Tkinter.PhotoImage data for dynamic CGI .gif without writing to file?

Will Stuyvesant hwlgw at hotmail.com
Thu Dec 26 06:35:26 EST 2002


# file: creategif.py
# A CGI script that dynamically creates a .GIF image.
# This *works*, but is ugly: it has to create a file to get to the data.
#
# Writing and then reading a created .GIF file is a rather ugly solution...
# What is needed is Tkinter.PhotoImage.__data__() and .__len()__
# methods: who can write them?

from Tkinter import *

class MyGif:

    def __init__(self, color="", width=1, height=1):        
        self.image = PhotoImage(width=width, height=height)
        if color:
            for row in range(height):
                for col in range(width):
                    if row % 2 == 0:
                        if col % 2 == 0:
                            self.colorXY(color, col, row)
                
    def colorXY(self, color, col, row):
        self.image.put(color, (col, row))

    def __len__(self):
        return 342  # XXX  compute .gif size of self.image
                    # HOW TO DO THIS?


Tk()
image = MyGif(width=100, height=10, color="blue")

# I want to avoid this file creation:
image.image.write('test.gif')
fp = open('test.gif')
fdata = fp.read()

print '''\
Content-type: image/gif
Content-length: %s

%s''' % (len(fdata), fdata)
   


'''
other files:

----- cgiserver.py ------------------------------------------------
from CGIHTTPServer import test as serve
if __name__=='__main__': serve()
-------------------------------------------------------------------

----- test.html ---------------------------------------------------
<title>Displaying a dynamically created .GIF file</title>
<p>
Here comes the image:
<p>
<IMG SRC="http://localhost:8000/htbin/creategif.py"
als="">
-------------------------------------------------------------------


CGI on your windows laptop mini tutorial:
=========================================

    From cmd.exe:
Create a directory c:\cgi and cd there and put the 3 files there.
Create a directory (c:\cgi\) htbin and move your cgi script there.
Stay in directory c:\cgi and run the cgi server.

c:>mkdir cgi
c:>cd cgi
c:\cgi>mkdir htbin
c:\cgi>move creategif.py htbin
c:\cgi>cgiserver.py

    From another cmd.exe:
c:\cgi>test.html

'''



More information about the Python-list mailing list