Displaying an array as an image
fredrik at pythonware.com
Sat Nov 20 06:03:37 EST 1999
David Fenyes <dfenyes at flash.net> wrote:
> Is there an easy way to display a 2D array as an image using just
> Tkinter? I'm aware of a TK Image canvas widget, but can't find docs
> on how to use it or what an 'image' is supposed to consist of. I'm
> just evaluating numerical python as a platform and this is a pretty
> important functionality that is not really addressed in any of the
> readily available docs.
see the attached message. and remember that
python.org's search facility is your friend.
> PIL and friends seem to be more oriented to reading/writing image
> formats, and are not really what I'm looking for.
since you obviously don't know what PIL can do for
you, how can you be so sure?
and the demo scripts in the source distribution.
From: "Fredrik Lundh" <fredrik at pythonware.com>
Subject: Re: Tkinter PhotoImage Question
Date: Fri, 12 Nov 1999 16:06:38 +0100
Ivan Van Laningham <ivanlan at callware.com> wrote:
> Ok, I've managed to create an image, using xx=PhotoImage(file), and I
> can retrieve pixel values from that image using xx.get(x,y).
> Now I want to use the put() method to write a pixel.
> How do I do that? I would have expected that put() would take a string
> of pixel values for the data argument, but that doesn't seem to be the
> case. ...
from the eff-bot archives:
# create a greyscale ramp using pure Tkinter
# fredrik lundh, january 1998
# fredrik at pythonware.com
from Tkinter import *
# must initialize interpreter before you can create an image
root = Tk()
data = range(256) # 0..255
im = PhotoImage(width=len(data), height=1)
# tkinter wants a list of pixel lists, where each item is
# a tk colour specification (e.g. "#120432"). map the
# data to a list of strings, and convert the list to the
# appropriate tuple structure
im.put( (tuple(map(lambda v: "#%02x%02x%02x" % (v, v, v), data)),) )
# resize the image to the appropriate height
im = im.zoom(1, 32)
# and display it
w = Label(root, image=im, bd=0)
# after playing with this a little, you'll love
# the Python Imaging Library.
More information about the Python-list