colors of GIF images

Bengt Richter bokr at oz.net
Sat Dec 7 16:06:42 EST 2002


On 7 Dec 2002 06:33:00 -0800, mis6 at pitt.edu (Michele Simionato) wrote:

>Given a GIF image, for each pixel I would like to estract a tuple (x,y,c) 
>where x and y are the pixel coordinates and c is the pixel color.
>How can I do that ? Speed is not an issue, I only need it for small images.
>I guess I need PIL, or maybe I can go without PIL (which would be preferable).
>Any suggestion ? TIA,
>
>                    Michele

You can do more efficient things with PIL directly, I think, but this may
be enough to get you started? (basic pattern except printit() borrowed from
D:\Python-2.2.2\Demo\tkinter\guido\imagedraw.py):

BTW, I recently built PIL and installed it, so Tkinter could possibly be
picking up GIF support automatically from that. I seem to remember not being
able to read GIF images some time ago. Or maybe it's standard now.

===< printgif.py >=================================================
"""Print ascii raster of gif"""
import sys
if len(sys.argv)<2:
    print """
Usage: printgif.py path_to_gif_file
       then click image with left mouse button to print."""
    raise SystemExit

from Tkinter import *
def main():
    global img, w ,h
    filename = sys.argv[1]
    root = Tk()
    img = PhotoImage(file=filename)
    w, h = img.width(), img.height()
    canv = Canvas(root, width=w, height=h)
    canv.create_image(0, 0, anchor=NW, image=img)
    canv.pack()
    canv.bind('<Button-1>', printit)
    root.mainloop()

def printit(event):
    # find most common color and assume it's background
    # print it as . and all else as @
    d = {}
    for y in xrange(h):
        for x in xrange(w):
            try:
                k = img.get(x,y)
                d[k] +=1
            except KeyError:
                d[k] = 1
    counts = [(v,k) for k,v in d.items()]
    counts.sort()
    bg = counts[-1][1]  # assume most common is background
    for y in xrange(h):
        print ''.join(['@.'[img.get(x,y)==bg] for x in xrange(w)])

main()
===================================================================

[13:05] C:\pywk\tkint>printgif.py

Usage: printgif.py path_to_gif_file
       then click image with left mouse button to print.

[13:05] C:\pywk\tkint>printgif.py D:\Python22\Doc\icons\next.gif
................................
................................
................................
................................
................................
................@@@@@@@@........
.................@@@@@@@@.......
.................@@@@@@@@@......
..................@@@@@@@@......
...................@@@@@@@@.....
....................@@@@@@@@....
.....................@@@@@@@@...
......................@@@@@@@@..
..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.
..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.
.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.
.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@..
....................@@@@@@@@@...
...................@@@@@@@@@....
..................@@@@@@@@......
.................@@@@@@@@.......
................@@@@@@@@........
...............@@@@@@@@.........
.............@@@@@@@@@..........
............@@@@@@@@@...........
................................
................................
................................
................................
................................
................................

Regards,
Bengt Richter



More information about the Python-list mailing list