Generating a spacer gif on the fly

Will Stuyvesant hwlgw at hotmail.com
Wed Oct 9 12:55:36 EDT 2002


[Martin Franklin]
> forgot to mention the PhotoImage.write() method that will write out a gif 
> file.....

OOH I LOVE IT!
Create your own art with any algorithm!


# adapted code from Martin Franklin

from Tkinter import *

class Spacer:
    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 getSpacer(self):
        return self.image

    def colorXY(self, color, col, row):
        self.image.put(color, (col, row))

    def write(self):
        self.image.write('tst.gif')


if __name__=="__main__":
    root=Tk()
    image=Spacer(width=100, height=10, color="red")
    l1=Label(root, text="Image here>")
    l1.pack(side="left")
    l=Label(root, image=image.getSpacer())
    l.pack(side="left")
    l1=Label(root, text="<Image here")
    l1.pack(side="left")
    image.write()
    root.mainloop()

'''
  "Doctor, we did good, didn't we?"
  "Perhaps. Time will tell. Always does."
    -- Ace and the Doctor, in Ben Aaronovitch's _Remembrance of
       the Daleks_
'''



More information about the Python-list mailing list