-3

I can't get this image to be displayed in a tkinter gui because it doesn't recognize the image file or directory, here is my code:

import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()

path = "krebs.jpg"
img = ImageTk.PhotoImage(Image.open(path))
img.pack()

root.mainloop()

and here is the picture in question: Krebs security pic

Zach Goodman
  • 45
  • 1
  • 7

2 Answers2

2

I bet your current working directory isn't what you think it is. Try:

import os
os.getcwd()

Make sure that directory (printed by os.getcwd()) has your image in it (i.e. go to that folder and check to see if your image is there). If not, specify the absolute path to your image:

path = C:/Users/<user_name>/... .../krebs.jpg

or whatever the full path to the image is on your OS.

Alternatively, you could move the image to the folder indicated by os.getcwd().

Sometimes I make a typo when defining paths in python, because very simple IDEs (like IDLE) don't have in-line auto-complete. Here's a trick I use all the time to make sure my absolute path doesn't have a typo:

import tkinter.filedialog as fd
path = fd.askopenfilename()

It will open a familiar file opening dialog. Use the file dialog window to find your file, then click "Open", and you'll have the full path saved to variable "path".

mRotten
  • 447
  • 1
  • 4
  • 11
  • 1
    if your image is in the same directory as your source code, you could do something like: https://stackoverflow.com/a/1270970/1358308 – Sam Mason Oct 23 '18 at 16:40
  • That worked in order to select the picture I needed. However, after copying the picture to the directory it showed, it still won't automatically display the picture without manually selecting it – Zach Goodman Oct 23 '18 at 16:47
  • I wouldn't incorporate the fd.askopenfilename() into your program unless you want the filedialog to run every time. Do that in the python shell, and then copy/paste that path into your .py file. – mRotten Oct 23 '18 at 16:54
  • 1
    Edit: describing the full path to the file under path worked, thanks! And by manually selecting I meant when "askopenfilename()" is ran, it directs me to the documents page of my pc where I was able to manually click the picture and use it. Apologies for the lack of clarity, I'm still pretty new to programming :) – Zach Goodman Oct 23 '18 at 16:55
  • How is it that I solved the problem, but my answer wasn’t the accepted one? – mRotten Oct 30 '18 at 20:40
0

Well I believe there is a simpler solution and one that is better overall. Python can use . to tell the code that the file you are looking for is in your same directory as your python code. This way you do not need to select a file manually or use a complete file path name.

2nd I noticed you are trying to pack() an image into the window however you cannot do this. Instead add it to a label or canvas. Just make sure your image file is in the same folder as your .py file.

Try this.

import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()

path = "./krebs.jpg"
img = ImageTk.PhotoImage(Image.open(path))
lbl = tk.Label(root, image=img)
lbl.pack()

root.mainloop()

I used a simple small red square image I use for testing but here is the results:

enter image description here

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79