an error occured to add picture in python

2024/11/15 21:29:42

I am new in python and I am using python 3.5 version. I want to add photo to python, and here's the code I wrote:

from tkinter import *
win=Tk()
win.title("Python Image")canvas=Canvas(win,width=500,height=500)
canvas.pack()my_image=PhotoImage(file="C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg")
canvas.create_image(0,0,anchor=NW,image=my_image)win.mainloop()

But when I run it, the following error occurred:

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
================ RESTART: C:\Users\LABE-2\Desktop\rakibul.py ================
Traceback (most recent call last):File "C:\Users\LABE-2\Desktop\rakibul.py", line 8, in <module>my_image=PhotoImage(file="C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg")File "C:\Users\LABE-2\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3539, in __init__Image.__init__(self, 'photo', name, cnf, master, **kw)File "C:\Users\LABE-2\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3495, in __init__self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
>>> 
Answer

Photoimage can natively only load png and pgm&ppm images (http://effbot.org/tkinterbook/photoimage.htm).

You can load other image formats via PIL. For python3 use Pillow like this:

from PIL  import Image, ImageTk
from tkinter import Tk,Canvas,NW
win=Tk()
win.title("Python Image")canvas=Canvas(win,width=500,height=500)
canvas.pack()# use your path here ...
my_image = ImageTk.PhotoImage(Image.open(r"some.jpg"))
canvas.create_image(0,0,anchor=NW,image= my_image )win.mainloop()

You could have found all this information in NorthCats answer to How do I insert a JPEG image into a python Tkinter window? as well.

https://en.xdnf.cn/q/120401.html

Related Q&A

Implementing stack in python by using lists as stacks [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Addition function in Python is not working as expected

I tried to create a function using which I want to do mathematical operations like (Addition and Multiplication), I could able to prompt the values and when I insert the values result is not returning …

Probability Distribution Function Python

I have a set of raw data and I have to identify the distribution of that data. What is the easiest way to plot a probability distribution function? I have tried fitting it in normal distribution. But …

how to convert a np array of lists to a np array

latest updated: >>> a = np.array(["0,1", "2,3", "4,5"]) >>> a array([0,1, 2,3, 4,5], dtype=|S3) >>> b = np.core.defchararray.split(a, sep=,) >…

Regex stemmer code explanation

Can someone please explain what does this code do?def stemmer(word):[(stem,end)] = re.findall(^(.*ss|.*?)(s)?$,word)return stem

Scraping data from a dynamic web database with Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

Python representation of floating point numbers [duplicate]

This question already has answers here:Floating Point Limitations [duplicate](3 answers)Closed 10 years ago.I spent an hour today trying to figure out whyreturn abs(val-desired) <= 0.1was occasional…

How to grep only duplicate key:value pair in python dictionary?

I have following python dictionary.a={name:test,age:26,place:world,name:test1}How to grep only duplicate key:value pair from the above?Output should be: "name: test and name:test1"

IndentationError - expected an indented block [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 7 months ago.I get the IndentationError: expected an indented block. I was tryi…

No axis named 1 for object type class pandas.core.frame.DataFrame

I created a DataFrame and I am trying to sort it based on the columns. I used the below code.frame.sort_index(axis=1)But this is causing the below errors------------------------------------------------…