Python images display - python

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.

Related

How can I fix an Image error with GUIzero on 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.

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

How to save image data from clipboard to a file in Python 3 on Debian, using tkinter?

I'm trying to use the tkinter solution for obtaining clipboard image data copied from GIMP, but cannot make it work, saving the data to file:
from tkinter import Tk
r = Tk()
r.withdraw()
clip = r.clipboard_get(type="image/png")
r.update()
r.destroy()
with open("testbytes.png", mode="bw+") as f:
f.write(clip.encode())
When I try to open the testbytes.png file, the Image Viewer reports a fatal error, not a PNG file. I obtained the type parameter for the clipboard_get() call with r.selection_get(selection='CLIPBOARD', type='TARGETS'), which returned:
'TIMESTAMP TARGETS MULTIPLE SAVE_TARGETS image/png image/bmp image/x-bmp image/x-MS-bmp image/x-icon image/x-ico image/x-win-bitmap image/vnd.microsoft.icon application/ico image/ico image/icon text/ico image/tiff image/jpeg '
I think the format of the data on the clipboard is PNG. I've also tried JPEG, BMP and TIFF, but they result in similar errors.
What am I doing wrong?
Using a conversion method, obtained in a separate SO question, for the hexdump of PNG data that tkinter provides from the clipboard, the correct code is:
from tkinter import Tk
r = Tk()
r.withdraw()
clip = r.clipboard_get(type="image/png")
r.update()
r.destroy()
# Convert hexdump to bytes
clip = bytes([eval(h) for h in clip.strip().split(' ')])
with open("testbytes.png", mode="bw+") as f:
f.write(clip)
Apart from writing out a PNG file, the data may also be loaded with the pillow module (formerly known as PIL):
import io
from PIL import Image
cf = io.BytesIO(clip)
cim = Image.open(cf)
cim.show()
As far as I've been able to determine, this is the best method of reading a PNG file from the clipboard into Python 3 on Linux (Debian).
I have created a tool for this intended for windows. The code could be used for debian as well. This script monitors for change in your clipboard and pops up a tkinter window asking you the filename to be used for saving the image, if it is not present in the folder. The image is then saved into the folder as a PNG file. It is particularly helpful with the windows shortcut Windows + Shift + S used to snip the screen.
Please find it here: https://github.com/Mitzzzzz/Clipboard-Image-Saver

"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()

how to embed an image in a text widget

I know it is possible to embed an image in a Tkinter text widget, but I've been unable to find some simple example code.
Specifically, I need to embed a jpg, so according to the docs I think I need to use the photoimage class
I tried to use this:
img=PhotoImage ( file=imgfn )
text.image_create(image=img)
where imgfn is the image filename, and text is my text widget,
but I get "_tkinter.TclError: couldn't recognize data in image file ..."
thanks for any help!
PhotoImage only handles GIF and PGM/PPM files. In order to use JPEG with Tkinter, you can use the Python Imaging Library (PIL) to create a PhotoImage.
from PIL import Image, ImageTk
img = Image.open("yourimg.jpg")
photoImg = ImageTk.PhotoImage(img)
Alternatively you could just one of the other supported formats for PhotoImage if possible.

Categories

Resources