Skip to content

Instantly share code, notes, and snippets.

@nakagami
Last active March 2, 2023 23:58
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nakagami/3764702 to your computer and use it in GitHub Desktop.
Save nakagami/3764702 to your computer and use it in GitHub Desktop.
Image viewer by PIL and TK
#!/usr/bin/env python
##############################################################################
# Copyright (c) 2012 Hajime Nakagami<nakagami@gmail.com>
# All rights reserved.
# Licensed under the New BSD License
# (http://www.freebsd.org/copyright/freebsd-license.html)
#
# A image viewer. Require Pillow ( https://pypi.python.org/pypi/Pillow/ ).
##############################################################################
import PIL.Image
try:
from Tkinter import *
import tkFileDialog as filedialog
except ImportError:
from tkinter import *
from tkinter import filedialog
import PIL.ImageTk
class App(Frame):
def chg_image(self):
if self.im.mode == "1": # bitmap image
self.img = PIL.ImageTk.BitmapImage(self.im, foreground="white")
else: # photo image
self.img = PIL.ImageTk.PhotoImage(self.im)
self.la.config(image=self.img, bg="#000000",
width=self.img.width(), height=self.img.height())
def open(self):
filename = filedialog.askopenfilename()
if filename != "":
self.im = PIL.Image.open(filename)
self.chg_image()
self.num_page=0
self.num_page_tv.set(str(self.num_page+1))
def seek_prev(self):
self.num_page=self.num_page-1
if self.num_page < 0:
self.num_page = 0
self.im.seek(self.num_page)
self.chg_image()
self.num_page_tv.set(str(self.num_page+1))
def seek_next(self):
self.num_page=self.num_page+1
try:
self.im.seek(self.num_page)
except:
self.num_page=self.num_page-1
self.chg_image()
self.num_page_tv.set(str(self.num_page+1))
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('Image Viewer')
self.num_page=0
self.num_page_tv = StringVar()
fram = Frame(self)
Button(fram, text="Open File", command=self.open).pack(side=LEFT)
Button(fram, text="Prev", command=self.seek_prev).pack(side=LEFT)
Button(fram, text="Next", command=self.seek_next).pack(side=LEFT)
Label(fram, textvariable=self.num_page_tv).pack(side=LEFT)
fram.pack(side=TOP, fill=BOTH)
self.la = Label(self)
self.la.pack()
self.pack()
if __name__ == "__main__":
app = App(); app.mainloop()
@Thilroy
Copy link

Thilroy commented Jun 27, 2019

Really nice work ! Thanks ! It's the best and simpler image viewer using PIL/Tkinter that I've found so far.
Actually, I'd like to use it to make some sort of frame-by-frame animation (speed is not an issue here) on a Raspberry pi.
Therefore, I'll have to remove the filedialog part to automate it and I'll try to find how to control the display of the next image.
I will also set in in fullscreen mode. Being a beginner, it's going to be a little challenge for me so if you've got time to spare for advice, it'll be always welcome !
Thierry

@MitchDresdner
Copy link

I agree nice Gist to get rolling with! I'm looking at writing an image and mpeg viewer to run on my Pi.

Thinking of using this Gist as a starter, or this Gist here for a slideshow viewer:
https://gist.github.com/endolith/50e50ad6b2b16d319e5f7df0acc184d1

adding time.sleep(3) before the print keeps it from zipping by.

Recently did a blog on a camera i'de like to pull images and video from using Pi 4. The dialog works fine on my Pi.
https://bestow.info/getting-started-with-the-g007-spy-camera/

@frank-fvs
Copy link

Thilroy

¡Muy buen trabajo! Gracias ! Es el mejor y más simple visor de imágenes con PIL / Tkinter que he encontrado hasta ahora.
En realidad, me gustaría usarlo para hacer algún tipo de animación cuadro por cuadro (la velocidad no es un problema aquí) en una Raspberry pi.
Por lo tanto, tendré que eliminar la parte de diálogo de archivo para automatizarlo e intentaré encontrar cómo controlar la visualización de la siguiente imagen.
También lo estableceré en modo de pantalla completa. Siendo un principiante, va a ser un pequeño desafío para mí, así que si tienes tiempo de sobra para un consejo, ¡siempre será bienvenido!
Thierry

Did you finish the project you had in mind? I want to do the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment