943,573 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 888
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 26th, 2009
0

for loop with Tk PIL image loading

Expand Post »
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)
  1. for file in files[:]:
  2. image = Image.open(filedir+"/"+file)
  3. ##((width, height))
  4. image.thumbnail((160, 240))
  5. photo = ImageTk.PhotoImage(image)
  6. label = Label(image=photo)
  7. label.image = image # keep a reference!
  8. label.pack()
  9.  
  10.  
  11. print files
  12. return
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingofkya is offline Offline
13 posts
since Sep 2009
Sep 27th, 2009
0

Re: for loop with Tk PIL image loading

Hint:
Looks to me like you are creating the same label over and over again.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 27th, 2009
0

Re: for loop with Tk PIL image loading

Ok yeah what would be the right way to set it up for multiply images

would a do something like
count += 1
then lable+(count) = whatever
Last edited by kingofkya; Sep 27th, 2009 at 3:35 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingofkya is offline Offline
13 posts
since Sep 2009
Sep 27th, 2009
0

Re: for loop with Tk PIL image loading

You could take each labelobject an append it to a list.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 27th, 2009
0

Re: for loop with Tk PIL image loading

Try something like this:
python Syntax (Toggle Plain Text)
  1. label = list(range(len(files)))
  2. for k, fname in enumerate(files):
  3. image = Image.open(filedir+"/"+fname)
  4. ##((width, height))
  5. image.thumbnail((160, 240))
  6. photo = ImageTk.PhotoImage(image)
  7. label[k] = Label(image=photo)
  8. label[k].image = image # keep a reference!
  9. #label[k].pack() # pack when you want to display it
  10.  
  11.  
  12. #print files
  13. return ...
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 30th, 2009
0

Re: for loop with Tk PIL image loading

thanks I was as trying to do image.append and it wasent working
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingofkya is offline Offline
13 posts
since Sep 2009
Sep 30th, 2009
0

Re: for loop with Tk PIL image loading

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




Python Syntax (Toggle Plain Text)
  1. label = list(range(len(files)))
  2. for k, fname in enumerate(files):
  3. image = Image.open(filedir+"/"+fname)
  4. ##((width, height))
  5. image.thumbnail((160, 240))
  6. photo = ImageTk.PhotoImage(image)
  7. label[k] = Label(picSet, image=photo)
  8. label[k].image = image # keep a reference!
  9. label[k].pack() # pack when you want to display it
  10. picSet.mainloop()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingofkya is offline Offline
13 posts
since Sep 2009
Sep 30th, 2009
0

Re: for loop with Tk PIL image loading

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)
  1. from os import *
  2. from Tkinter import *
  3. import tkFileDialog
  4. from PIL import Image, ImageTk
  5.  
  6. root = Tk()
  7. root.title("BIEC Picture Viewer")
  8. title = Label(root, text="Select a Set of images")
  9.  
  10.  
  11.  
  12. filedir = tkFileDialog.askdirectory()
  13. files = listdir(filedir)
  14. for file in files[:]:
  15. if(file.split(".")[-1].upper() != 'JPG'):
  16. files.remove(file)
  17. label = list(range(len(files)))
  18. for k, fname in enumerate(files):
  19. image = Image.open(filedir+"/"+fname)
  20. ##((width, height))
  21. image.thumbnail((160, 240))
  22. photo = ImageTk.PhotoImage(image)
  23. label[k] = Label(root, image=photo)
  24. label[k].image = image # keep a reference!
  25. label[k].pack() # pack when you want to display it
  26. picSet.mainloop()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kingofkya is offline Offline
13 posts
since Sep 2009
Oct 1st, 2009
0

Re: for loop with Tk PIL image loading

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)
  1. label_list = []
  2. for k, fname in enumerate(files):
  3. image = Image.open(filedir+"/"+fname)
  4. ##((width, height))
  5. image.thumbnail((160, 240))
  6. photo = ImageTk.PhotoImage(image)
  7. this_label = Label(root, image=photo)
  8. this_label.pack() # pack when you want to display it
  9. label_list.append( this_label )
  10. picSet.mainloop()
Last edited by woooee; Oct 1st, 2009 at 1:59 am.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Oct 1st, 2009
0

Re: for loop with Tk PIL image loading

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
Last edited by vegaseat; Oct 1st, 2009 at 10:10 am.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: * Edit post * Delete post * Report this post * Reply with quote Ho
Next Thread in Python Forum Timeline: complicated list return?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC