Yogaraj
Yogaraj

Reputation: 320

Tkinter not showing images in directory

I have a folder with images, I'm using tkinter and PIL to show the images in a directory. But, whenever I run the following code it doesn't shows any image. Here is the code I tried,

from Tkinter import *
import os
from PIL import Image, ImageTk    

def getFileName(image):
    print str(image)

def CropManual():
    global outputFile
    #getCrop(outputFile)
    print "Crop Manual"

def showImages(folder):
    print "loading....", folder
    gtk = Tk()
    gtk.wm_title("Images")

    row, col = 0,0
    for images in os.listdir(folder):
        print images
        im = Image.open(images)
        #im = im.resize(250, 250, Image.ANTIALIAS)
        tkimage = ImageTk.PhotoImage(im)
        handler = lambda img = images : getFileName(img)
        imageButton = Button(gtk, image=tkimage, command=handler)
        imageButton.image=tkimage
        imageButton.grid(row=row+1, column=col+1, padx=3, pady=3)
        row +=1; col+=1;
    userCrop = Button(gtk, text="Crop Manually?", command=CropManual)
    userCrop.grid(row=row+1, column=col+1, padx=3, pady=3)
    gtk.mainloop()

showImages("/home/yogaraj/Music/Image1487915648.54/")

The image folder is here.

Here is the error I get

It shows no such file or directory though the file is present. Can anyone help me out with this issue?

Upvotes: 0

Views: 1275

Answers (1)

eyllanesc
eyllanesc

Reputation: 243947

You must pass the absolute path of the image

Change

im = Image.open(images)

to

im = Image.open(folder + images)

or better

im = Image.open(os.path.join(folder, images))

Upvotes: 2

Related Questions