Reputation: 154
The given code is not working, as it gives the error of file not found for 'haridwar.jpg' though I tried putting it in in Python35 and also on desktop. Please help
import tkinter as tk
from PIL import Image,ImageTk
root = tk.Tk()
root.title("display image")
im=Image.open("haridwar.jpg")
photo=ImageTk.PhotoImage(im)
cv = tk.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(10, 10, image=photo, anchor='nw')
root.mainloop()
Upvotes: 0
Views: 20905
Reputation: 15236
Judging by your question you might not have the file in a good location; also you are not providing a path to those locations. So lets break it down a little.
You said you placed the image in the Python35
folder. Without knowing more I would imagine you are talking about the python default directory located somewhere like C:\program files\Python35
.
If this is the case then change the line:
im=Image.open("haridwar.jpg")
To this:
im=Image.open("C:\program files\Python35\haridwar.jpg")
though this is not a good place for your image. We will get to that in a sec.
As you stated you also tried your desktop. So you would want to provide a path to your desktop.
Something like this:
im=Image.open("C:/Users/your_user_folder/Desktop/haridwar.jpg")
This is also not a great place for your file.
Lets try something else. Lets put the file inside your working python directory.
For example if your main.py file is located inside of
"C:/myworkspace/my_program/main.py"
then you can place that image in the same my_program
folder and your code should work as is.
If you want to have a folder just for images you could have one in a directory that looks like this:
"C:/myworkspace/my_program/my_images/haridwar.jpg"
In this case you can provide a short path like this:
im=Image.open("./my_images/haridwar.jpg")
notice the .
before the /my_image
folder. This is used to tell Python it can look inside its current working directory for the folder.
Upvotes: 7
Reputation: 1283
I tried your code using a directory/filename i know was correct, and it works. You have an error in the spelling of your directory/filename or you got the directory wrong.
Make sure you have the directory and file name correct.
For example, I have "Image.jpg" on my desktop
import tkinter as tk
from PIL import Image,ImageTk
root = tk.Tk()
root.title("display image")
im=Image.open("C:/Users/<myname>/Desktop/Image.jpg") #This is the correct location and spelling for my image location
photo=ImageTk.PhotoImage(im)
cv = tk.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(10, 10, image=photo, anchor='nw')
root.mainloop()
Upvotes: 4