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: kingofkya is an unknown quantity at this point 
Solved Threads: 0
kingofkya kingofkya is offline Offline
Newbie Poster

for loop with Tk PIL image loading

 
0
  #1
Sep 26th, 2009
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: for loop with Tk PIL image loading

 
0
  #2
Sep 27th, 2009
Hint:
Looks to me like you are creating the same label over and over again.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: kingofkya is an unknown quantity at this point 
Solved Threads: 0
kingofkya kingofkya is offline Offline
Newbie Poster

Re: for loop with Tk PIL image loading

 
0
  #3
Sep 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: for loop with Tk PIL image loading

 
0
  #4
Sep 27th, 2009
You could take each labelobject an append it to a list.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: for loop with Tk PIL image loading

 
0
  #5
Sep 27th, 2009
Try something like this:
  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 ...
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: kingofkya is an unknown quantity at this point 
Solved Threads: 0
kingofkya kingofkya is offline Offline
Newbie Poster

Re: for loop with Tk PIL image loading

 
0
  #6
Sep 30th, 2009
thanks I was as trying to do image.append and it wasent working
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: kingofkya is an unknown quantity at this point 
Solved Threads: 0
kingofkya kingofkya is offline Offline
Newbie Poster

Re: for loop with Tk PIL image loading

 
0
  #7
Sep 30th, 2009
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




  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()
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: kingofkya is an unknown quantity at this point 
Solved Threads: 0
kingofkya kingofkya is offline Offline
Newbie Poster

Re: for loop with Tk PIL image loading

 
0
  #8
Sep 30th, 2009
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

  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()
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,028
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 290
woooee woooee is offline Offline
Veteran Poster

Re: for loop with Tk PIL image loading

 
0
  #9
Oct 1st, 2009
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
  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.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: for loop with Tk PIL image loading

 
0
  #10
Oct 1st, 2009
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC