_tkinter.TclError: format error in bitmap data - python

This line:
bitmap = Tkinter.BitmapImage(file="logo.bmp")
Gives me this error:
File "gpm.py", line 314, in <module>
bitmap = Tkinter.BitmapImage(file=LOGO_PATH)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 3365, in __init__
Image.__init__(self, 'bitmap', name, cnf, master, **kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 3262, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: format error in bitmap data
I have no clue how to fix this. It doesn't work with any .bmp file.

The tkinter BitmapImage takes an X11 bitmap file as an argument. This is not the same format as a windows .bmp file. Make sure you're using the correct type of file. Typically, the suffix of an X11 bitmap file is .xbm.

Related

Python Not Opening Image Files

Code:
import turtle as trtl
wn = trtl.Screen()
wn.setup(width = 1.0, height = 1.0)
wn.bgpic("Underseav2_BG.png")
wn.mainloop()
Error Message:
Traceback (most recent call last):
File "c:\Users\Natha\Desktop\ATCS Create Task\main_game.py", line 7, in <module>
wn.bgpic("Underseav2_BG.png")
File "C:\Users\Natha\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1482, in bgpic
self._bgpics[picname] = self._image(picname)
File "C:\Users\Natha\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 478, in _image return TK.PhotoImage(file=filename, master=self.cv) File "C:\Users\Natha\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Natha\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4038, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "Underseav2_BG.png": no such file or directory
This is the Code I wrote and the error message I got. I have the image file in the same folder (shown in this image here:[Code&Folder]
as my code, yet Python says they are not in the same directory. What has happened here, and is there anything I can do to fix it?
Thanks!
If you download the image from the Internet, make sure that its original format is png.
If you download the image as a jpg and modify it to png, I think this will not work.
https://pixabay.com/illustrations/optical-flare-png-5397752/
I downloaded an image from this site in png format, the code worked successfully.
if you're using Mac, right click -> Quick Actions -> Convert Image -> Choose the Format to "PNG"

hey ,i have created a program in gui tkinter in python and couldnt upload image as label/icon eventhough they are under same project

from tkinter import *
window = Tk()
window.geometry("500x500")
icon = PhotoImage(file="X.png")
window.iconphoto(True,icon)
window.mainloop()
#my code
#error
Traceback (most recent call last):
File "C:\Users\priya\PycharmProjects\pythonProject2\main.py", line 5, in <module>
icon = PhotoImage(file="X.png")
File "C:\Users\priya\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\priya\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4038, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "X.png"
It seems that the file X.png is not a valid PNG image file, try viewing it in a image viewer and check its filetype via https://www.checkfiletype.com/ or using the file command via WSL.

Imaging problems with Python 3.6

I am creating artificial intelligence to play Chinese checkers with Python, but I can't even get an image of a board to show up!
I am using this code:
from tkinter import *
root = Tk()
board = PhotoImage(file="board.ppm")
root.mainloop()
I get the following error:
Traceback (most recent call last):
File "/Users/GAMEKNIGHT7/Desktop/genius hour/chineseCheckersAI(genius hour).py", line 3, in <module>
board = PhotoImage(file="board.ppm")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "board.ppm"
I placed the code file in the same file as the image. What happened? How can I fix it?
Try running the following in Terminal:
file board.ppm
If it says anything with the word ASCII in it, that means your image is uncompressed ASCII (NetPBM type=3) and Tkinter will not like it:
board.ppm: Netpbm image data, size = 10 x 10, pixmap, ASCII text
if your file is correct, it will report rawbits (NetPBM type=6):
board.ppm: Netpbm image data, size = 10 x 10, rawbits, pixmap
If you want to convert from ASCII/P3 to binary/rawbits/P6, you could install ImageMagick with homebrew like this:
brew install imagemagick
Then convert like this:
convert board.ppm -compress lossless board.ppm

Tkinter PhotoImage error "encountered an unsupported criticial chunk type "iDOT""

I am trying to show an image on screen with python and tkinter, but when I run it, it gives an error in the PhotoImage object.
This is my code:
from tkinter import *
root = Tk()
photo = PhotoImage(file="devil.png")
label = Label(root, image=photo)
label.pack()
root.mainloop()
The image file is in the same folder as the .py file.
And it gives this error:
Traceback (most recent call last):
File "C:/Users/MyUsername/PycharmProjects/GUI test/home.py", line 5, in <module>
photo = PhotoImage(file="devil.png")
File "C:\Users\MyUsername\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\MyUsername\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3498, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: encountered an unsupported criticial chunk type "iDOT"
Does anyone know how to fix this?
The chunk type of image you're using, 'iDOT', is not a registered PNG chunk. So, you should replace the image with an appropriate one.
This can help you understand what actually error is about

_tkinter.TclError: bitmap "pyimage2" not defined

I want to display an icon on a menu bar, so using this information, I coded this:
img = Image.open("help.png")
menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img))
I got this error:
Traceback (most recent call last):
File "mine.py", line 67, in <module>
m.menus(root)
File "mine.py", line 55, in menus
menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img))
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2699, in add_cascade
self.add('cascade', cnf or kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2696, in add
self._options(cnf, kw))
_tkinter.TclError: bitmap "pyimage2" not defined
How to fix this ?
To display a PhotoImage you should use the image attribute, not bitmap.
You can also simply open an image file directly using ImageTk.PhotoImage(file='...')
So you can use the following code to display your image in the menu:
img = ImageTk.PhotoImage(file="help.png")
menubar.add_cascade(label="Help", menu=helpmenu, image=img)

Categories

Resources