Solution when the image cannot be displayed with tkinter [python]

When I try to display an image with tkinter

"~ .png " could not be recognized.

Since it became, a memorandum

environment

Python 3.7.9 Tvl/Tk 8.6 Pillow==7.2.0

Check the version of tkinter

import tkinter
tkinter._test() 

Main subject

Code in question


import tkinter
root = tkinter.Tk()

#Front omitted

canvas = tkinter.Canvas(root, width=800, height=600)
canvas.pack()

img = tkinter.PhotoImage(file="my_picture.png ")
canvas.create_image(400, 300, image=img)


#Rear omitted

Apparently, tkinter's PhotoImage only touches on a fairly limited extension.

Solved with pillow

Resolution code


import tkinter
from PIL import ImageTk, Image

root = tkinter.Tk()

#Front omitted

canvas = tkinter.Canvas(root, width=400, height=600, bg="skyblue")
canvas.pack()

image = Image.open("my_picture.png ")
photo = ImageTk.PhotoImage(image, master=root)
canvas.create_image(200, 300, image=photo)


#Rear omitted

that's all

Recommended Posts

Solution when the image cannot be displayed with tkinter [python]
Investigation when import cannot be done with python
Python note: When the pip command cannot be used
Python modules with "-(hyphen)" cannot be removed
Address to the bug that node.surface cannot be obtained with python3 + mecab
Be careful of the type when making an image mask with Numpy
I tried "smoothing" the image with Python + OpenCV
Project cannot be created with Python3.5 (Windows) + django1.7.1
When Html cannot be output with Jupyter Notebook
When contour map cannot be drawn with APLpy
I tried "differentiating" the image with Python + OpenCV
GUI image cropping tool made with Python + Tkinter
I tried "binarizing" the image with Python + OpenCV
A solution to the problem that the Python version in Conda cannot be changed
Solution when aws-cli cannot be executed on Mac virtual environment (after installing Python 3.8)
How to display in the entire window when setting the background image with tkinter
Correspondence memo when the direction key cannot be used on the python command line
Image processing with Python
Extract the table of image files with OneDrive & Python
[Python] Get the numbers in the graph image with OCR
Convert the image in .zip to PDF with Python
[android] When AVD cannot be used with permission denied
Causes when python version cannot be changed using pyenv
Run the output code with tkinter, saying "A, pretending to be B" in python
Workaround for the problem that UTF-8 Japanese mail cannot be sent with Flask-Mail (Python3)
Solved the problem that the image was not displayed in ROMol when loaded with PandasTools.LoadSDF.
When using tf.print (), the contents of the tensor cannot be displayed if it is inside f-string.
Solution when you want to use cv_bridge with python3 (virtualenv)
Image processing with Python (Part 2)
I tried to find the entropy of the image with python
Programming with Python and Tkinter
I tried "gamma correction" of the image with Python + OpenCV
Options when installing libraries that cannot be piped with pyenv
Sorting image files with Python (2)
Sorting image files with Python (3)
Master the type in Python? (When should type check be done)
Video cannot be loaded with Spyder in Python development environment
Create Image Viewer with Tkinter
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Image processing with Python (Part 3)
When Times New Roman cannot be used with matplotlib (Ubuntu)
Run Label with tkinter [Python]
Error when playing with python
When USB cannot be formatted
Call the API with python3.
Linux delete command when only the folder cannot be deleted
When pydub cannot be installed
[Python] Image processing with scikit-image
[For IT beginners] What to do when the rev command cannot be used with Git Bash
Workaround for the problem that? Is displayed when checking the version when using non-standard perl | python with pkg version
Life game with Python [I made it] (on the terminal & Tkinter)
Be careful when specifying the default argument value in Python3 series
Be careful of LANG for UnicodeEncodeError when printing Japanese with Python 3
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Make a Spinbox that can be displayed in Binary with Tkinter
ZipArchive couldn't be used with the Laravel image launched by ConoHa.
Tkinter canvas (window) does not start when running Python 3.7.4 with Atom
[Python] Error and solution memo when using venv with pyenv + anaconda
Parallel computing (pathos) when dealing with objects that cannot be pickled