Question:
I am working on a tkinter application where I need to display images in the GUI. While loading .png images works perfectly fine, I am encountering difficulties when attempting to load .jpg images using tk.PhotoImage
.
Here is the snippet of code I am using:
`...
# Function for the additional button click action
def additional_function():
try:
image_path = "output/exp/img.jpg"
image = tk.PhotoImage(file=image_path)
img_label.config(image=image)
img_label.image = image # to keep a reference
except Exception as e:
print(f"Error loading image: {e}")
button2 = tk.Button(root, text="Show The Result", command=additional_function, bg="red", highlightthickness=0)
button2.config(font=("Helvetica", 14, "bold"), fg="white")
button2.place(relx=(start_x + end_x) / 2 / root.winfo_screenwidth(), rely=(sum(row_heights[:y]) + row_heights[y] + 600) / root.winfo_screenheight(), anchor=tk.CENTER)`
...`
However, when attempting to load a .jpg image, I encounter the following error:
Error loading image: couldn't recognize data in image file "output/exp/img.JPG"
I would appreciate any insights or suggestions on resolving this problem. Thank you in advance.
Ahmed Alsayadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.