Python Not Opening Image Files - python

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"

Related

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: format error in bitmap data

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.

Python Pillow - broken data stream

Pillow looks to be the solution to a lot of my problems, but I can't get it to work correctly. I think there might be something that I'm missing but I can't find any solutions that work.
from PIL import Image
img = Image.open("base64 (1).png")
print(img.size)
print(img.format)
img.show()
I have this set up to load an image, print its size and format, and then show the image. When I run the code, I get this:
(1920, 1080)
PNG
Traceback (most recent call last):
File "C:/Users/##########/PycharmProjects/weatherCanvas/imageTest.py", line 7, in <module>
img.show()
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1736, in show
_show(self, title=title, command=command)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2449, in _show
_showxv(image, **options)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2454, in _showxv
ImageShow.show(image, title, **options)
File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 51, in show
if viewer.show(image, title=title, **options):
File "C:\Python27\lib\site-packages\PIL\ImageShow.py", line 75, in show
image = image.convert(base)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 844, in convert
self.load()
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 250, in load
raise_ioerror(e)
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 59, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
The same thing happens if I replace img.show() with saving it. I know that PIL and Pillow save a temporary file in order to do the show() method, so I think the problem is somewhere in that.
Is there something I'm missing here?

Categories

Resources