Imaging problems with Python 3.6 - python

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

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.

Trying to insert an image with tkinter

I am trying to build my first gut using tkinter (still super new to python). I have no tried different ways to insert an image but none of them worked. I think I don't even get ImageTk imported... The error message in this case is "TclError: cannot use geometry manager pack inside . which already has slaves managed by grid". Can someone help me do it properly? The image file is in the same directory as the .py file
import tkinter as tk
from tkinter import *
from PIL import ImageTk,Image
root = tk.Tk()
photo = PhotoImage(file = "6.gif")
tk.Label(root, image = photo).pack()
root.mainloop()
traceback:
runfile('/Users/maj-brittbuchholz/Desktop/image practice tkinter.py', wdir='/Users/maj-brittbuchholz/Desktop')
Traceback (most recent call last):
File "", line 1, in
runfile('/Users/maj-brittbuchholz/Desktop/image practice tkinter.py', wdir='/Users/maj-brittbuchholz/Desktop')
File "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/maj-brittbuchholz/Desktop/image practice tkinter.py", line 13, in
tk.Label(root, image = photo).pack()
File "/opt/anaconda3/lib/python3.7/tkinter/init.py", line 2766, in init
Widget.init(self, master, 'label', cnf, kw)
File "/opt/anaconda3/lib/python3.7/tkinter/init.py", line 2299, in init
(widgetName, self._w) + extra + self._options(cnf))
TclError: image "pyimage22" doesn't exist
There are several things wrong:
The tk.label has a master called win. Either you are not showing us the entire code, or it is undefined. If it is defined somewhere else, then this is fine.
The error says you can't pack it because something is using grid. This means that there is something else that has grided slaves.
Other than that, your program is fine. You may be wrongly assumong that this is where the error is coming from, or maybe not.

Python WAND WandRuntimeError

I have the following piece of python code to convert PDF to JPG.
with Img(filename=pdfName, resolution=300) as pic:
pic.compression_quality = self.compressionQuality
pic.background_color = Color("white")
pic.alpha_channel = 'remove'
pic.save(filename=output)
My problem is, with a large PDF file (10mb) I have the following error :
File "/home/nathan/PycharmProjects/oc_for_maarch/worker.py", line 44, in <module>
launch(args)
File "/home/nathan/PycharmProjects/oc_for_maarch/src/main.py", line 105, in launch
q = process(args, path + file, Log, Separator, Config, Image, Ocr, Locale, WebService, q)
File "/home/nathan/PycharmProjects/oc_for_maarch/src/process/OCForMaarch.py", line 48, in process
Image.pdf_to_jpg(file + '[0]')
File "/home/nathan/PycharmProjects/oc_for_maarch/src/classes/Images.py", line 36, in pdf_to_jpg
self.save_img_with_wand(pdfName, self.jpgName)
File "/home/nathan/PycharmProjects/oc_for_maarch/src/classes/Images.py", line 46, in save_img_with_wand
with Img(filename=pdfName, resolution=300) as pic:
File "/home/nathan/Documents/OpenCV/lib/python3.7/site-packages/wand/image.py", line 6406, in __init__
self.read(filename=filename, resolution=resolution)
File "/home/nathan/Documents/OpenCV/lib/python3.7/site-packages/wand/image.py", line 6799, in read
raise WandRuntimeError(msg)
wand.exceptions.WandRuntimeError: MagickReadImage returns false, but did raise ImageMagick exception. This can occurs when a delegate is missing, or returns EXIT_SUCCESS without generating a raster.
I checked a little on Internet, and for what I've seen the problems was related to ghostscript but It's installed
I have the problem on Debian 10 and Ubuntu 19.04 using Python 3.7
EDIT : If I put the resolution to 100 instead of 300, I didn't have the issue
When you rasterize at a high density, you will make a potentially very large dimension image from your PDF. So it sounds like you may be running out of RAM. If so, then you need to edit your ImageMagick policy.xml file to allow for more ram or map space. See policy.xml at https://imagemagick.org/script/resources.php. It controls your resources which you can view with the command line command:
convert -list resource

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

Categories

Resources