How can I fix an Image error with GUIzero on Python - python

I'm a beginner in Python and I was wondering why my "app" wont display my image but it'll display everything else fine. I'm using GUIzero. Image file is in the same folder as the code file.
Error: Traceback GUIZERO ERROR
Image import error - ‘couldn’t
recognize data in image file “lad.png”
Check the file path and image type is GIF/PNG
Here's my code ↓
from guizero import App, Text, Picture
app = App("Wanted!")
app.bg = "#FFFBB0"
wanted_text = Text(app, "WANTED")
wanted_text.text_size = 50
wanted_text.font = "Times New Roman"
cat = Picture(app, image="lad.png")
app.display()
Thanks!

I was facing the same issue and then I changed my Image file and downloaded a new png file from the internet and put it in the same folder in which my program was.. and boom magic happened and my program run well.
the first image was actually a jpg image and I converted it into png using an online converter I think it doesn't converted well into png.

Related

Why is PIL .save not working with my output_name ? (ValueError: unknown file extension: .jpg)

I made a little app to change the colors of some pictures. I use PIL to change the pixels colors, and Tkinter to make the GUI. I made a Console version , which is working perfectly, and a GUI version which is basically the same but with a tkinter interface. In the Console version, i ask the user for an output file name with
output_name = input('Your output file name'). here, the user enters something like "mypicture.jpg"
Then, with PIL i create my new image and call it canva, add the pixels and use
canva.save(output_name)
Everything works perfectly and my image is saved with its name, "mypicture.jpg" in the same directory as the python file. But in my GUI version, I use Tkinter with
text = Text(fenetre,height=3,width=40)
and then
text.place(relx=0.5, rely=0.4, anchor=N)
After, I assign output_name with the content of the textbox (the users enters what he wants in it like "mypicture.jpg")
output_name = str(text.get(1.0, 'end'))
But when I try to launch canva.save(output_name) , I get the following error :
raise ValueError(f"unknown file extension: {ext}") from e
ValueError: unknown file extension: .jpg
I don't understand why this error happens and how to fix it because output_name is exactly the same text "mypicture.jpg" in both cases !

i get an error when trying to add an image to python tkinter

hi so have this image in the same directory as my .py file . and i changed the image from "background.jpg" to "background.gif" because i heard it helps
when i hover my cursor over the file=background.gif it show me the image so it can read it
but when i try to run . it give me an error and says "couldn't recognize data in image file "background.gif""
from tkinter import *
root = Tk()
photo = PhotoImage(file="background.gif")
label = Label(root, image=photo)
label.pack()
root.mainloop()
code
error
I think the problem here really is(as acw1668 mentioned) because you didn't use an image converter to convert the image, instead just simply renamed the file to .gif, which is not the correct way of changing file formats and can damage the file at times. Using an jpg to gif converter online can clear this issue and give you a gif that works :D

"couldn't recognize data in image file ..." Why do I (sometimes) get this Error-message?

I wanted to implement some images in my Python-Tkinter application. I used the PhotoImage instance of Tkinter for this. It all worked perfectly whith the first picture: "RR.gif, a GIF image made with MS Paint. The second image: "Tank.gif", unfortunately caused some troubles. This picture was made (also in MS Paint) immediately after the first one and was also stored in GIF format. The images are located in the same folder so I assume the path is correct for both of them. However, the second label, (which contains the second image) gives the following error:
_tkinter.TclError: couldn't recognize data in image file "Tank.gif"
Which I think is strange since both images files are very similar. Does anyone know why I am getting this error? and how to solve it?
This is the simplest code in which the problem occurs.
from Tkinter import *
root = Tk()
icon = PhotoImage(file="RR.gif")
tank = PhotoImage(file="Tank.gif")
label_ico = Label(root, image=icon)
label_tan = Label(root, image=tank)
label_ico.pack()
label_tan.pack()
root.mainloop()

PIL loading jpg

I'm using PIL to load a jpg file and display it in a label widget. At first, I got "decoding error" from python and found this post on stack overflow - How can I install PIL on mac os x 10.7.2 Lion - and it's resolved the decoding error. However, the label doesn't display any image, just a white area. This is the code for loading image -
script, file = argv
self.orgimg = Image.open(file)
#Original Image
img = ImageTk.PhotoImage(self.orgimg)
Label(self.root, image=img).grid(row=0,column=0,padx=5,pady=5)
I've got the feeling that the image has been garbage collected. Check out this: http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm
If you store the image in a local variable it will be garbage collected when the function returns.
Maybe you should call:
self.orgimg.load()
to actualy load the bitmap-information.
Also there's a caveat according to this site which resemles your problem.

Python images display

How can I create a python script that runs through the images (1.jpeg-n.jpeg) in a directory on a mac and displays them in a browser OR via another python program?
Do I import a file to python and than display in browser?
Do I extract the file names 1,2,3,4,5 and add that to a list, which I give to another function that calls a browser and displays?
Any help would be great.
Thanks!
Using Tkinter and PIL for this purpose is pretty trivial. Add muskies example to the information from this thread that contains this example:
# use a Tkinter label as a panel/frame with a background image
# note that Tkinter only reads gif and ppm images
# use the Python Image Library (PIL) for other image formats
# free from [url]http://www.pythonware.com/products/pil/index.htm[/url]
# give Tkinter a namespace to avoid conflicts with PIL
# (they both have a class named Image)
import Tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
root.title('background image')
# pick an image file you have .bmp .jpg .gif. .png
# load the file and covert it to a Tkinter image object
imageFile = "Flowers.jpg"
image1 = ImageTk.PhotoImage(Image.open(imageFile))
# get the image size
w = image1.width()
h = image1.height()
# position coordinates of root 'upper left corner'
x = 0
y = 0
# make the root window the size of the image
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
# root has no image argument, so use a label as a panel
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
# put a button on the image panel to test it
button2 = tk.Button(panel1, text='button2')
button2.pack(side='top')
# save the panel's image from 'garbage collection'
panel1.image = image1
# start the event loop
root.mainloop()
Of course if you're more familiar with another GUI, go ahead and adapt the example, it shouldn't take much.
You first have to find all image filenames. You can use os.listdir(...) to get all files in a certain directory, or glob.glob(...) to find all files matching a certain pattern.
Showing the images is the second and more challenging part of this. The first option is to open the images in an external program, this can be a web browser. On (most) platforms a command firefox 1.jpeg will open the image 1.jpeg in the Firefox browser. You can use the subprocess module to execute such commands. If you want to show them using a nice GUI, you have to create a GUI using some framework and use this. But if you are a beginner this might be a little bit too difficult for you.
For example:
import glob
import subprocess
files = glob.glob('dir/*.jpeg')
for file in files:
subprocess.call(['firefox', file])
muksie's answer already contains very useful advice. If don't want to write the HTML file yourself or want something a little fancier you could use a small script I wrote for the MDP library. This basically allows you to just do:
import slideshow
slideshow.show_image_slideshow(filenames, image_size=(80,60))
This will create an HTML slideshow and open it in your browser. You can grab just the needed files here (only templet.py and the two slideshow files are needed), which might be better for you than getting the full library.
It might be a lot easier to just generate a static web page with pictures than building a GUI for displaying pictures.
You can just generate a hmtl page, put the images on it and start your web browser with the newly created html file. this gives you some possibilities for layout as well.
If you just want a picture in the browser then muksie gave a working example.

Categories

Resources