Last active
March 2, 2023 23:58
-
-
Save nakagami/3764702 to your computer and use it in GitHub Desktop.
Image viewer by PIL and TK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
############################################################################## | |
# Copyright (c) 2012 Hajime Nakagami<[email protected]> | |
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thilroy
Did you finish the project you had in mind? I want to do the same.