1

I have got my file working on my program. However, I am not sure how to load an image from a file that is in a specific folder. So far is my following code, which only can obtain the image directly in the same folder as the program file itself.

from tkinter import *

root = Tk()

canvas = Canvas(root, width=500, height=500)
canvas.pack()

imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)

root.mainloop()

I had found some info on this YouTube video about how to link the file to a specific folder, but it only tells the whole destination. I cannot guarantee that the whole folder destination stuff shown by that video will always work if I move the folder containing the program to a different destination; moving that folder will break the image links in the program.

Is there some way of loading an image from a folder that is in the same folder as the program is? I recall that HTML could do this kind of thing too, but I don't know whether Python could do this either.

3
  • You can try using a relative path like file="./giftest.gif" Commented Sep 5, 2018 at 1:57
  • The relative path stuff is starting to solve my problem. However, I also need to know how to specify a particular folder relative to my image. Commented Sep 5, 2018 at 2:07
  • ***Your answer is here:***<br> stackoverflow.com/questions/47357090/… Commented Aug 16, 2021 at 16:07

2 Answers 2

3

If the absolute path is not fixed, you can always build the file path in your code.

This would combine the absolute path of the current directory and the relative path of your file. As long as the relative location of the image is always the same to the python script, this would always work.

import os
directory_path = os.path.dirname(__file__)
file_path = os.path.join(directory_path, 'relative_path/file')

Note that the directory_path is the directory path of your python script.

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

4 Comments

Does "relative_path" refer to the folder name?
Yes. Sorry for the confusion, I used relative_path in case it is a nested folder. And remember you do not need a "/" in front of the folder name.
Is it possible to do this for multiple different files? And how would I adjust it if I have multiple files to be displayed?
Can you clarify your use case? Are your files scattered in different locations or are they in the same folder say ./images/?
1

You can specify the folder in your python file containing the image like "folder/giftest.gif". Here is a sample:

from tkinter import *

root = Tk()

canvas = Canvas(root, width=500, height=500)
canvas.pack()

imagetest = PhotoImage(file="folder/subfolder/giftest.gif")  #python file is not in "folder" but "folder" is in your python file directory
canvas.create_image(250, 250, image=imagetest)

root.mainloop()

3 Comments

Is "folder" the name of the folder the program itself is in? And "subfolder" is the name of the folder's folder?
Actually, when I tested out the program, it seems that I only need to put in the folder's-folder's name and then the filename to load my image from the folder's folder; trying to put my folder's name and my folder's-folder's name would just give an error.
@RonZhang Python file is not in the folder but folder is in the python file directory. Hope you understand.

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.