469,949 Members | 2,124 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,949 developers. It's quick & easy.

viewing generated images

Hi,

I'm trying to write a simple image viewer using Tkinter and ImageTk from
the PIL toolkit. The images are stored in memory and I don't want to
create temporary files for them.

My code looks like this :
from Tkinter import *
import Image
import ImageTk

class Viewer(Frame):
def __init__( self, master=None ):
Frame.__init__( self, master )
self.grid()
self.createWidgets()

def createWidgets(self):
self.image_size = (50,50)
self.pilim = Image.new( "1", self.image_size )

# Generate a blank image
f = lambda(x): 0
Image.eval( self.pilim, f )

self.bmi = ImageTk.BitmapImage( image = self.pilim,
foreground = 'black' )
self.canvas = Canvas( width=100,
height = 100,
bg = 'white' )
self.quitButton = Button( self,
text="Quit",
command=self.quit )
self.quitButton.grid()
self.canvas.grid()
im_id = self.canvas.create_bitmap( 0,
0,
anchor = 'nw',
bitmap = self.bmi )
viewer = Viewer()
viewer.master.title("Test viewer")
viewer.mainloop()

The create_bitmap call fails with the following error :

_tkinter.TclError: bitmap "pyimage2" not defined

I've seen a couple of mentions of this problem but with no solution given
other than to write the image to a temporary file, which I don't want to
do.

I'd appreciate it if anyone could point me to a different approach for
this that works (and doesn't involve creating temporary files). I'd be
open to trying a different GUI/imaging package if anyone has suggestions
for one that would be better suited to this sort of application.

Otherwise I'm also interested if anyone has any ideas about the root cause
of this problem because if I don't find some work around I intend to try to
fix it in Tkinter.

Thanks,
Tim
Jul 19 '05 #1
2 2236
Tim Flynn wrote:
def createWidgets(self):
self.image_size = (50,50)
self.pilim = Image.new( "1", self.image_size )

# Generate a blank image
f = lambda(x): 0
Image.eval( self.pilim, f )
I'm not sure what you think that line is doing, but it probably
doesn't do what you think it does.

try changing the Image.new call to

self.pilim = Image.new( "1", self.image_size, 0 )

instead.
self.bmi = ImageTk.BitmapImage( image = self.pilim,
foreground = 'black' ) self.canvas = Canvas( width=100,
height = 100,
bg = 'white' )
self.quitButton = Button( self,
text="Quit",
command=self.quit )
self.quitButton.grid()
self.canvas.grid()
im_id = self.canvas.create_bitmap( 0,
0,
anchor = 'nw',
bitmap = self.bmi )


it's a terminology confusion: for historical reasons, Tkinter distinguishes between
bitmaps and images (this separation comes from the X window system). Bitmap-
Image creates an image.

changing to create_image should fix the problem.

(if you're doing an image viewer, you may want to use PhotoImage
instead of BitmapImage, btw).

</F>

Jul 19 '05 #2
Fredrik Lundh wrote:
Tim Flynn wrote:
def createWidgets(self):
self.image_size = (50,50)
self.pilim = Image.new( "1", self.image_size )

# Generate a blank image
f = lambda(x): 0
Image.eval( self.pilim, f )


I'm not sure what you think that line is doing, but it probably
doesn't do what you think it does.

try changing the Image.new call to

self.pilim = Image.new( "1", self.image_size, 0 )

instead.
self.bmi = ImageTk.BitmapImage( image = self.pilim,
foreground = 'black' )

self.canvas = Canvas( width=100,
height = 100,
bg = 'white' )
self.quitButton = Button( self,
text="Quit",
command=self.quit )
self.quitButton.grid()
self.canvas.grid()
im_id = self.canvas.create_bitmap( 0,
0,
anchor = 'nw',
bitmap = self.bmi )


it's a terminology confusion: for historical reasons, Tkinter
distinguishes between
bitmaps and images (this separation comes from the X window system).
Bitmap- Image creates an image.

changing to create_image should fix the problem.

(if you're doing an image viewer, you may want to use PhotoImage
instead of BitmapImage, btw).

</F>


That fixed the problem I was having and your right I was confused about
how to create the image but I'm sure I'll be able to figure that out now
that I'm getting some output.

Thanks much.

Tim

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by IkBenHet | last post: by
2 posts views Thread by Tony Moaikel | last post: by
1 post views Thread by Gary | last post: by
10 posts views Thread by NH | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.