Created
June 16, 2018 04:31
-
-
Save dheaney/097d4801b36f696d22cf46170b82013f to your computer and use it in GitHub Desktop.
viewer2.py
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
from Tkinter import * | |
from PIL import Image, ImageTk | |
import glob | |
# https://stackoverflow.com/questions/17351524/tkinter-pil-resize-an-image-to-fit-a-label | |
class MyWindow(object): | |
def __init__(self): | |
self.image = None | |
self.root = Tk() | |
self.i = 0 | |
self.files = glob.glob('./*.png') + glob.glob('./*.jpg') + glob.glob('./*.jpeg') | |
#self.left = Button(self.root, text="Left", command=self.lclick) | |
#self.right = Button(self.root, text="Right", command=self.rclick) | |
self.scale = Scale(self.root, from_ = 0, to = len(self.files) - 1, orient=HORIZONTAL, command=self.change, length=500) | |
self.display = Canvas(self.root, width=500, height=500) | |
#self.left.pack() | |
#self.right.pack() | |
self.scale.pack() | |
self.display.pack() | |
self.update() | |
self.root.mainloop() | |
def change(self, index): | |
self.i = int(index) | |
self.update() | |
def lclick(self): | |
self.i = self.i + 1 | |
if self.i >= len(self.files): | |
self.i = 0 | |
self.update() | |
def rclick(self): | |
self.i = self.i - 1 | |
if self.i <= 0: | |
self.i = len(self.files) - 1 | |
self.update() | |
def update(self): | |
if self.image: | |
#self.label.destroy() | |
self.display.delete("IMG") | |
#self.photo = PhotoImage(file=self.files[self.i]) | |
self.original = Image.open(self.files[self.i]) | |
resized = self.original.resize((self.original.size[0] * 500 / self.original.size[1], 500), Image.ANTIALIAS) | |
self.image = ImageTk.PhotoImage(resized) | |
self.display.create_image(0, 0, image=self.image, anchor=NW, tags="IMG") | |
#self.label = Label(self.frame, image=self.photo, width=500, height=500) | |
#self.label.pack(); | |
m = MyWindow() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment