0

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()
4
  • When providing an error please provide the full trackback. Also welcome to stack overflow. If you are getting a file not found error it may be because your file is not in the same directory as your program. Can you provide the location of your main python file and the location of your image file? Commented Jun 12, 2017 at 17:19
  • For now, try using the full path to open the file. Also try printing the current working directory. Also, did you spell the name of the file correctly? did you mean hardware.jpg? You are missing an e. You have an extra i. (for English) Commented Jun 12, 2017 at 17:28
  • @SierraMountainTech, I tried putting it in the same working directory as my .py scipt "C:\Users\SAURAV DAS\AppData\Local\Programs\Python\Python35\projects", but still shows the same error Commented Jun 12, 2017 at 19:17
  • @RobertJacobs, The name of the file is correct, its named after a place as I don't have many images. My problem lies in specifying the path. Commented Jun 12, 2017 at 19:19

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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()

8 Comments

The downvote was because you did not address the OPs problem. All you did was copy the OPs code and tell him that the code works fine and they must have a spelling error. The issue is most likely a problem with the OPs understanding of how opening files works and not with the code itself so for an answer you should be breaking it down in a way the OP can learn from and not just provide a copy paste answer to a bad solution. (the bad solution is placing the file on the desktop vs inside the working directory)
Okay, I'll keep in mind to explain thoroughly why what they did was incorrect and correct/explain bad practice as well.
Sounds good. If you take the time to add some detail to your answer I will recant my downvote.
So your image and script are both in the above folder? are you using the exact same code as in your original post, or has anything changed, including the filename in this line: im=Image.open("haridwar.jpg") If you are using the full path to the file now, are you using / or \ in the path?
if the folder projects is where your main.py program is located then you can use this line: im=Image.open("haridwar.jpg") . This should get rid of the file not found error. keep in mind if you are using a full file path you may need to use \\ instead of \ for each folder seperator
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.