| | |
for loop with Tk PIL image loading
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
Ok my script takes a list of files picks out all jpgs and stores it in files[] now the problem is how do I display all thous images in tk gui window below is what i have that works for 1 image how do i make this work for basically an unlimited amount of images.
Python Syntax (Toggle Plain Text)
for file in files[:]: image = Image.open(filedir+"/"+file) ##((width, height)) image.thumbnail((160, 240)) photo = ImageTk.PhotoImage(image) label = Label(image=photo) label.image = image # keep a reference! label.pack() print files return
Try something like this:
python Syntax (Toggle Plain Text)
label = list(range(len(files))) for k, fname in enumerate(files): image = Image.open(filedir+"/"+fname) ##((width, height)) image.thumbnail((160, 240)) photo = ImageTk.PhotoImage(image) label[k] = Label(image=photo) label[k].image = image # keep a reference! #label[k].pack() # pack when you want to display it #print files return ...
drink her pretty
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
Is this line correct
label[k] = Label(picSet, image=photo)
if earlier in the code
picSet = Tk()
I get TclError: image "pyimage75" doesn't exist
However if i remove picSet it puts large spaces in the root window but no pics showup
label[k] = Label(picSet, image=photo)
if earlier in the code
picSet = Tk()
I get TclError: image "pyimage75" doesn't exist
However if i remove picSet it puts large spaces in the root window but no pics showup
Python Syntax (Toggle Plain Text)
label = list(range(len(files))) for k, fname in enumerate(files): image = Image.open(filedir+"/"+fname) ##((width, height)) image.thumbnail((160, 240)) photo = ImageTk.PhotoImage(image) label[k] = Label(picSet, image=photo) label[k].image = image # keep a reference! label[k].pack() # pack when you want to display it picSet.mainloop()
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
in the process of debugging i separated the problem of the script now this gives me the blank spaces in the root window but no images and no errors
Python Syntax (Toggle Plain Text)
from os import * from Tkinter import * import tkFileDialog from PIL import Image, ImageTk root = Tk() root.title("BIEC Picture Viewer") title = Label(root, text="Select a Set of images") filedir = tkFileDialog.askdirectory() files = listdir(filedir) for file in files[:]: if(file.split(".")[-1].upper() != 'JPG'): files.remove(file) label = list(range(len(files))) for k, fname in enumerate(files): image = Image.open(filedir+"/"+fname) ##((width, height)) image.thumbnail((160, 240)) photo = ImageTk.PhotoImage(image) label[k] = Label(root, image=photo) label[k].image = image # keep a reference! label[k].pack() # pack when you want to display it picSet.mainloop()
•
•
Join Date: Dec 2006
Posts: 1,028
Reputation:
Solved Threads: 290
Some ideas. First, tkinter does not display all image types. I do not think that it will display a JPG but am not sure. Convert some pictures to a gif and see it that works. Second, "file" is a reserved word and so should not be a variable name, Third, you are not iterating through the entire list of files because of the delete in the for loop. Append the names you want to keep to a second list. Also, you don't have to create a list of n elements, just append as you go along (see below). Finally, if you don't know about effbot, it is a good reference. http://effbot.org/tkinterbook/label.htm
Python Syntax (Toggle Plain Text)
label_list = [] for k, fname in enumerate(files): image = Image.open(filedir+"/"+fname) ##((width, height)) image.thumbnail((160, 240)) photo = ImageTk.PhotoImage(image) this_label = Label(root, image=photo) this_label.pack() # pack when you want to display it label_list.append( this_label ) picSet.mainloop()
Last edited by woooee; Oct 1st, 2009 at 1:59 am.
Linux counter #99383
Tkinter is somewhat finicky about persistent images. Take a look at this example where I had to create a photo list to make the label list work. Not quite sure how those two lists connect:
http://www.daniweb.com/forums/showth...48#post1001448
http://www.daniweb.com/forums/showth...48#post1001448
Last edited by vegaseat; Oct 1st, 2009 at 10:10 am.
May 'the Google' be with you!
![]() |
Similar Threads
- How to replace missing images with default image (PHP)
- displaying image at loading time & display message in success after any operation (JavaScript / DHTML / AJAX)
- Putting an image into a Tkinter thingy (Python)
- Positioning static image over scrolling images (HTML and CSS)
- Loading image to HTML (JavaScript / DHTML / AJAX)
- Frameset,Showing loading..Please wait message (ASP.NET)
- Image swap behaviour from Dreamweaver no longer works once uploaded (Site Layout and Usability)
- setTimeout prevents resetting of image (JavaScript / DHTML / AJAX)
- Code Snippet: Rotating an Image (Python) (Python)
Other Threads in the Python Forum
- Previous Thread: * Edit post * Delete post * Report this post * Reply with quote Ho
- Next Thread: complicated list return?
| Thread Tools | Search this Thread |
abrupt ansi anti approximation array assignment avogadro backend beginner binary bluetooth builtin calculator character chmod code converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary drive dynamic examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse mysqlquery number numbers output parsing path plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion scrolledtext sqlite statistics stdout string strings sum table terminal text textarea thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows write wxpython xlib






