Unable to use JPEG images in tkinter - python

My code:
from tkinter import *
mah_root = Tk()
mah_root.geometry("900x900")
photo = PhotoImage(file="2.jpeg")
varun_label = Label(image=photo)
varun_label.pack()
mah_root.mainloop()
Error:
Traceback (most recent call last):
File "/Users/dm19/Desktop/GUI/tut5.py", line 7, in <module>
photo = PhotoImage(file="2.jpeg")
File "/Users/dm19/opt/anaconda3/lib/python3.9/tkinter/__init__.py", line 4064, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Users/dm19/opt/anaconda3/lib/python3.9/tkinter/__init__.py", line 4009, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "2.jpeg"

Related

Open PIL image from zip (Kaggle competition)

I am trying to read an image from kaggle competition (It is an old competition, but I would like to practice):
https://www.kaggle.com/competitions/dogs-vs-cats-redux-kernels-edition
I am trying to read images from the training file zip using this code:
def get_files_names(zip_file_path):
with ZipFile(zip_file_path) as myzip:
return myzip.namelist()
def get_image(zip_path, image_name):
with ZipFile(zip_path) as myzip:
# print(myzip.namelist()[:10])
with myzip.open(image_name) as myfile:
# img = Image.open(myfile)
img = Image.open(myfile)
return img
names = get_files_names(train_file_path)
img = get_image(train_file_path, names[1])
img.show()
I am getting this error:
Traceback (most recent call last):
File "/cats_vs_dogs/unrelated_file.py", line 46, in <module>
img.show()
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 2205, in show
_show(self, title=title, command=command)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 3167, in _show
_showxv(image, **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 3181, in _showxv
ImageShow.show(image, title, **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 56, in show
if viewer.show(image, title=title, **options):
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 81, in show
return self.show_image(image, **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 107, in show_image
return self.show_file(self.save_image(image), **options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageShow.py", line 103, in save_image
return image._dump(format=self.get_format(image), **self.options)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/Image.py", line 636, in _dump
self.load()
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/ImageFile.py", line 247, in load
s = read(self.decodermaxblock)
File "/Users/user/.pyenv/versions/3.7.8-thesis/lib/python3.7/site-packages/PIL/JpegImagePlugin.py", line 400, in load_read
s = self.fp.read(read_bytes)
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 930, in read
data = self._read1(n)
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 998, in _read1
data += self._read2(n - len(data))
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 1030, in _read2
data = self._fileobj.read(n)
File "/Users/user/.pyenv/versions/3.7.8/lib/python3.7/zipfile.py", line 753, in read
self._file.seek(self._pos)
AttributeError: 'NoneType' object has no attribute 'seek'
If I extract the file into finder (using mac), then I see this image:
Also, if I try to convert the RGB image into into a numpy array np.array(img), I get this result:
What am I doing wrong?

Python error - Traceback (most recent call last)

I entered this code. I entered the example alike in the book where I could study Python.
from turtle import Screen, Turtle
import random
screen=Screen()
image1="C:\\rabbit.gif"
image2="C:\\turtle.gif"
screen.addshape(image1)
screen.addshape(image2)
t1=turtle.Turtle()
t1.shape(image1)
t1.pensize(5)
t1.penup()
t1.goto(-300, 0)
t2=turtle.Turtle()
t2.shape(image2)
t2.pensize(5)
t2.penup()
t2.goto(-300, -200)
t1.pendown()
t2.pendown()
t1.speed(1)
t2.speed(1)
for i in range(100):
d1=random.randint(1, 60)
t1.forward(d1)
d2=random.randint(1, 60)
t2.forward(d2)
I entered the code correctly, but there was an error.
Here is the code of the error.
Traceback (most recent call last):
File "D:/대학/파이썬프로그래밍기초/파이썬_과제(2018)/fd.py", line 7, in <module>
screen.addshape(image1)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1134, in register_shape
shape = Shape("image", self._image(name))
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 478, in _image
return TK.PhotoImage(file=filename, master=self.cv)
File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\USER\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 "C:\rabbit.gif"
I tried to find a way to correct the error, but I couldn't find it. I wonder where this code is wrong.

Python , Matplotlib problem with font_manager font

I have created a personal style (using different classes inherit from a generic base class)
recently after an upgrade python gives me this error:
Traceback (most recent call last):
File "/media/data/Programming/Python/ODEPyPackage/sys06.py", line 130, in <module>
main()
File "/media/data/Programming/Python/ODEPyPackage/sys06.py", line 95, in main
fig,axs = PalatinoItalic()(1,1,(12.0,4.0))#
File "/media/data/Programming/Python/ODEPyPackage/qualityPlot/qualityplot.py", line 547, in __init__
super().__init__(**self.parameters )
File "/media/data/Programming/Python/ODEPyPackage/qualityPlot/qualityplot.py", line 124, in __init__
super().__init__(**self.parameters )
File "/media/data/Programming/Python/ODEPyPackage/qualityPlot/qualityplot.py", line 71, in __init__
super().__init__(**self.parameters)
File "/media/data/Programming/Python/ODEPyPackage/qualityPlot/basequalityplot.py", line 30, in __init__
font_manager.fontManager.addfont(font_file)
File "/usr/lib/python3.9/site-packages/matplotlib/font_manager.py", line 1092, in addfont
font = ft2font.FT2Font(path)
RuntimeError: In FT2Font: Can not load face. Unknown file format.
this is the base class:
class BasePlot(metaclass=ABCMeta):
#abstractmethod
def __init__(self,**kwargs):
self.parameters = kwargs
self.nrows = None
self.ncols = None
self.figsize = None
font_dirs = ['/home/marco/.fonts', ]
font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list
font_list = font_manager.createFontList(font_files)
font_manager.fontManager.ttflist.extend(font_list)
may somebody help me?

'File not found' while importing gif for Tkinter.Label

I'm trying to import gif into a Tkinter.Label. I'm getting a file does not exist error. I have double checked the path and file name.
Below is the error message I get.
Traceback (most recent call last):
File "C:\Users\sachin\Desktop\Project California.py", line 39, in <module>
if __name__=='__main__': main()
File "C:\Users\sachin\Desktop\Project California.py", line 35, in main
feedback = Feedback(root)
File "C:\Users\sachin\Desktop\Project California.py", line 11, in __init__
self.logo = PhotoImage(file= "‪C:\\Users\\sachin\\Desktop\\signature.gif")
File "C:\Users\sachin\AppData\Local\Programs\Python\Python36
\lib\tkinter\__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "‪C:\Users\sachin\Desktop\signature.gif": no
such file or directory
Here is the code I used.
def __init__(self,master):
self.frame_header = ttk.Frame(master)
self.logo = PhotoImage(file= "‪C:\\Users\\sachin\\Desktop\\signature.gif")
ttk.Label(self.frame_header, image = self.logo)
I'm a noob in python programming. Apologies if the questions is too trivial.
I get the same error and I'm sure the path is right as well. In fact, it seems to find the file every other time it tries, with very similar code.
for r in range(0,5):
for c in range(1,4):
fn = file_name(c)
try:
photo = tk.PhotoImage(fn)
tk.Button(C, image = photo, width = "16", height = "16").grid(row = r,column = c)
except Exception as exception:

import mapclient ,and the error is that : TclError: out of stack space (infinite loop?)

import ee
import mapclient
ee.Initialize()
image = ee.Image('srtm90_v4')
map = mapclient.MapClient()
map.addOverlay(mapclient.MakeOverlay(image.getMapId({'min': 0, 'max': 30})))
enter image description here
the error codes as follow:
Exception in thread Thread-9:
Traceback (most recent call last):
File "D:\Anaconda2\lib\threading.py", line 801, in bootstrap_inner
self.run()
File "mapclient.py", line 440, in run
callback(image)
File "mapclient.py", line 215, in AddTile
newtile = ImageTk.PhotoImage(newtile)
File "D:\Anaconda2\lib\site-packages\PIL\ImageTk.py", line 113, in __init
self.photo = apply(Tkinter.PhotoImage, (), kw)
File "D:\Anaconda2\lib\lib-tk\Tkinter.py", line 3367, in __init
Image.init(self, 'photo', name, cnf, master, **kw)
File "D:\Anaconda2\lib\lib-tk\Tkinter.py", line 3321, in init
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: out of stack space (infinite loop?)
Exception in thread Thread-7:
Traceback (most recent call last):
File "D:\Anaconda2\lib\threading.py", line 801, in bootstrap_inner
self.run()
File "mapclient.py", line 440, in run
callback(image)
File "mapclient.py", line 215, in AddTile
newtile = ImageTk.PhotoImage(newtile)
File "D:\Anaconda2\lib\site-packages\PIL\ImageTk.py", line 113, in __init
self.photo = apply(Tkinter.PhotoImage, (), kw)
File "D:\Anaconda2\lib\lib-tk\Tkinter.py", line 3367, in __init
Image.init(self, 'photo', name, cnf, master, **kw)
File "D:\Anaconda2\lib\lib-tk\Tkinter.py", line 3321, in init
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: out of stack space (infinite loop?)
Exception in thread Thread-16:
Traceback (most recent call last):
File "D:\Anaconda2\lib\threading.py", line 801, in bootstrap_inner
self.run()
File "mapclient.py", line 440, in run
callback(image)
File "mapclient.py", line 215, in AddTile
newtile = ImageTk.PhotoImage(newtile)enter code here
File "D:\Anaconda2\lib\site-packages\PIL\ImageTk.py", line 113, in __init
self.photo = apply(Tkinter.PhotoImage, (), kw)
File "D:\Anaconda2\lib\lib-tk\Tkinter.py", line 3367, in __init
Image.init(self, 'photo', name, cnf, master, **kw)
File "D:\Anaconda2\lib\lib-tk\Tkinter.py", line 3321, in init
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: expected boolean value but got "??"

Categories

Resources