-1

A Screenshot of my folder and the script that causes trouble Hi guys, I want that my program opens the file with the PIL package "settingsicon.png" and with the usage of the path ./x but it does not work. It says "No such file or directory: './settingsicon.png'"

I try it with the libary os but it does not work ether.

import os
from PIL import Image
import customtkinter

# Get absolute file path
file_path = os.path.abspath('path/to/settingsicon.png')

# Load image
img = Image.open(file_path)
Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • Hey, from the screenshot, I can see that your working directory is `C:\Users\bayez` which is not the directory of your python file. You need first to change your working directory. And if that's vscode, I think you can just hit the "Run" button – Mina Melek May 12 '23 at 21:38
  • "./" doesn't necessarily represent the folder the script is in. It represents the current working directory which can and often is different. – Bryan Oakley May 13 '23 at 02:05

4 Answers4

0

You can extract the path of the current script from __file__ as discussed here:

https://stackoverflow.com/a/3430395/21012339

That way you won't have to cd into the folder each time.

If / when you organize your program into a package, there's an even more pythonic way:

from importlib.resources import files
file = files(__package__) / "myfile.xyz"
mytext = file.read_text()
myimage = Image.open(file) #may or may not work without converting to path string
Chr L
  • 89
  • 4
  • Is it possible without a new libary? – BetrickBZD May 16 '23 at 10:13
  • ```importlib``` is part of Python. The solution using ```__file__``` only imports standard libs, as well. I don't see how that would be problem. But as others pointed out, you could also stick to the ```os``` module and achieve the same. – Chr L May 16 '23 at 12:19
0

Either put the full path or execute it in the directory that has the script and Image. or make it use the script's directory in code like:

script_dir = os.path.dirname(os.path.abspath(__file__))

image_path = os.path.join(script_dir, "settingsicon.png")

settingsicon = customtkinter.CTkImage(dark_image=Image.open(image_path), size=(18, 18))

this should work, based on the code in your screenshot.

Klein
  • 41
  • 5
0

script_dir = os.path.dirname(os.path.abspath(file))

image_path = os.path.join(script_dir, "settingsicon.png")

settingsicon = customtkinter.CTkImage(dark_image=Image.open(image_path), size=(18, 18))

Thank you all for your help :)

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Blue Robin May 16 '23 at 14:33
-2

Edit: Remove typo error in line 9.

Try this:

Add this ImageTk.PhotoImage.

Snippet:

import tkinter as tk
import os
from PIL import ImageTk, Image

root = tk.Tk()
import customtkinter

# Get absolute file path
file_path = 'p1.png'

# Load image
img = ImageTk.PhotoImage(Image.open(os.path.abspath(file_path)))
 
tk.Label(root, image=img).pack()


root.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19