Last active
April 14, 2022 23:35
-
-
Save ryujaehun/3f93ccbeb6d578d259aa944a4c130e91 to your computer and use it in GitHub Desktop.
image_viewer.py
This file contains hidden or 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/python2 | |
import PIL.Image | |
try: | |
from Tkinter import * | |
import tkFileDialog as filedialog | |
except ImportError: | |
from tkinter import * | |
from tkinter import filedialog | |
import PIL.ImageTk | |
import glob ,os | |
import tkMessageBox | |
class App(Frame): | |
def open_dir(self): | |
directory = filedialog.askdirectory() | |
if directory == "": | |
tkMessageBox.showinfo('Error', "Directory is null") | |
self.dir_list=glob.glob(directory+'/*.jpg') | |
self.dir_list+=glob.glob(directory+'/*.jpeg') | |
self.dir_list+=glob.glob(directory+'/*.JPEG') | |
self.dir_list+=glob.glob(directory+'/*.JPG') | |
self.dir_list+=glob.glob(directory+'/*.png') | |
self.dir_list+=glob.glob(directory+'/*.gif') | |
self.dir_list+=glob.glob(directory+'/*.PNG') | |
self.dir_list+=glob.glob(directory+'/*.GIF') | |
self.__len=len(self.dir_list) | |
self.dir_list=sorted(self.dir_list,key=lambda x:int(x.split('/')[-1].split('.')[0])) | |
self.im = PIL.Image.open(self.dir_list[self.num]) | |
self.change() | |
self.file_name.set(self.dir_list[self.num].split('/')[-1].split('.')[0]) | |
return | |
def change(self): | |
if self.im.mode == "1": | |
self.img = PIL.ImageTk.BitmapImage(self.im, foreground="white") | |
else: | |
self.img = PIL.ImageTk.PhotoImage(self.im) | |
self.la.config(image=self.img, bg="#000000", | |
width=self.img.width(), height=self.img.height()) | |
def prev(self,*arg): | |
if self.num == -1: | |
self.num=self.__len-1 | |
self.num-=1 | |
self.im = PIL.Image.open(self.dir_list[self.num]) | |
self.change() | |
self.file_name.set(self.dir_list[self.num].split('/')[-1].split('.')[0]) | |
def remove(self,*arg): | |
os.remove(self.dir_list[self.num]) | |
self.dir_list.remove(self.dir_list[self.num]) | |
if self.num == self.__len-1: | |
self.num-=1 | |
self.__len-=1 | |
self.im = PIL.Image.open(self.dir_list[self.num]) | |
self.change() | |
self.file_name.set(self.dir_list[self.num].split('/')[-1].split('.')[0]) | |
def next(self,*arg): | |
if self.num +2== self.__len: | |
self.num=-1 | |
self.num+=1 | |
self.im = PIL.Image.open(self.dir_list[self.num]) | |
self.change() | |
self.file_name.set(self.dir_list[self.num].split('/')[-1].split('.')[0]) | |
def __init__(self, master=Tk()): | |
Frame.__init__(self, master) | |
self.master.title('python viewer') | |
self.num=0 | |
self.file_name = StringVar() | |
fram = Frame(self) | |
self.btn1=Button(fram, text="Open File Directory", command=self.open_dir).pack(side=LEFT) | |
self.btn2=Button(fram, text="Prev", command=self.prev).pack(side=LEFT) | |
self.btn3=Button(fram, text="Next", command=self.next).pack(side=LEFT) | |
self.btn3=Button(fram, text="Remove", command=self.remove).pack(side=LEFT) | |
self.master.bind('<Left>', self.prev) | |
self.master.bind('<Right>', self.next) | |
self.master.bind('<Left>', self.prev) | |
self.master.bind('<Delete>', self.remove) | |
Label(fram, textvariable=self.file_name).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() |
you can change by pressing a mouse key using this mouse key.
if you change bind to , it will be work well.
<Button-1> Button 1 is the leftmost button, button 2 is the middle button
(where available), and button 3 the rightmost button.
<Button-1>, <ButtonPress-1>, and <1> are all synonyms.
For mouse wheel support under Linux, use Button-4 (scroll
up) and Button-5 (scroll down)
@Dhesmok
And the easy way to load two image is make frame twice.
But it need some error handing about border of img list
@Dhesmok And the easy way to load two image is make frame twice. But it need some error handing about border of img list
Thanks a lot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's really great. I don't know anything about programming. Can you load two images and change them by pressing a mouse key?