【Pythonエラー対策】_tkinter.TclError: couldn’t recognize data in image file

Python用モジュール「Tkinter」で_tkinter.TclError: couldn’t recognize data in image fileエラーが出た場合の対処方法についてソースコード付きでまとめました。

## _tkinter.TclError: couldn’t recognize data in image file

Pythonの標準モジュール「Tkinter」のtkinter.PhotoImage(file)を用いて、画像を表示させようとすると

_tkinter.TclError: couldn't recognize data in image file

というエラーが表示されることがあります。

これは、tkinter.PhotoImageは、「GIF」「PGM/PPM」のみ対応なためです。
そのため、JPGやPNG画像を読み込むと先程のエラーが発生します。
「GIF」「PGM/PPM」以外の画像形式も読み込みたい場合、Pillow等の画像処理ライブラリで別途読み込む必要があります。

関連記事
1 【Python/Tkinter】GUIアプリ作成入門【Python】GUIライブラリの比較・おすすめ
2 Python入門 基本文法
Python
スポンサーリンク

コメント