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.

Ron Zhang
  • 195
  • 1
  • 3
  • 17
  • You can try using a relative path like `file="./giftest.gif"` – prithajnath Sep 05 '18 at 01: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. – Ron Zhang Sep 05 '18 at 02:07
  • ***Your answer is here:***
    https://stackoverflow.com/questions/47357090/tkinter-error-couldnt-recognize-data-in-image-file/68805933#68805933
    – team meryb Aug 16 '21 at 16:07

2 Answers2

2

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.

Victor Wong
  • 3,457
  • 1
  • 18
  • 31
  • Does "relative_path" refer to the folder name? – Ron Zhang Sep 05 '18 at 04:32
  • 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. – Victor Wong Sep 05 '18 at 04:43
  • Is it possible to do this for multiple different files? And how would I adjust it if I have multiple files to be displayed? – Ron Zhang Sep 05 '18 at 05:02
  • Can you clarify your use case? Are your files scattered in different locations or are they in the same folder say ./images/? – Victor Wong Sep 05 '18 at 07:15
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()
Nouman
  • 6,947
  • 7
  • 32
  • 60
  • Is "folder" the name of the folder the program itself is in? And "subfolder" is the name of the folder's folder? – Ron Zhang Sep 05 '18 at 21:05
  • 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. – Ron Zhang Sep 05 '18 at 21:22
  • @RonZhang Python file is not in the `folder` but `folder` is in the python file directory. Hope you understand. – Nouman Sep 06 '18 at 11:52