Image Viewer Using Tkinter In Python

Hello, Everyone…

In this post, we are going to build an Image Viewer In Python Using Tkinter.

Tkinter is an inbuilt module in Python by which you can make a GUI.

We are going to use a special module pillow that helps to manipulate the image.

To install this pillow in your system use:-

pip install pillow.

Before continuing please go through these two modules specially Tkinter because we will use two more features of Tkinter.

Also, take some knowledge about the class and object because we need this.

So let’s move to the module-wise or function-wise explanation of the program.

def __init__(self,master):
 self.master = master 
 self.c_size = (700,500) 
 self.setup_gui(self.c_size) 
 self.img=None

This is the init function inside our class Image_Viewer, when we make an object of our class this init function will be the function that will be invoked first implicitly and set the size of windows as per given, set the image as none.

def setup_gui(self,s): 
 Label(self.master,text='Image Viewer',pady=5,bg='white', font=('Arial',30)).pack() 
 self.canvas = Canvas(self.master,height=s[1],width=s[0], bg='Black',bd=10,relief='ridge') 
 self.canvas.pack() 
 txt = ''' By Shrimad Mishra on behaf of CodeSpeedy ''' 
 self.wt = self.canvas.create_text(s[0]/2-270,s[1]/2,text=txt ,font=('',30),fill='white') 
 f=Frame(self.master,bg='white',padx=10,pady=10) 
 Button(f,text='Open Image',bd=2,fg='white',bg='black',font=('',15) ,command=self.make_image).pack(side=LEFT) 
 f.pack()

In this function, we have mainly designed the look of our GUI

def make_image(self): 
 try: 
  File = fd.askopenfilename()  
  self.pilImage = Image.open(File) 
  re=self.pilImage.resize((700,500),Image.ANTIALIAS) 
  self.img = ImageTk.PhotoImage(re) 
  self.canvas.delete(ALL) 
  self.canvas.create_image(self.c_size[0]/2+10,self.c_size[1]/2+10, anchor=CENTER,image=self.img) 
  self.status['text']='Current Image:'+File 
 except: 
  ms.showerror('Error!','File type is unsupported.')

In this function, we save the file uploaded by the user in the File variable if the file is supported we show the picture after resizing it if the file is not supported then we simply tell the user that the file is not supported.

So full coding part is here :-

from tkinter import * 
from tkinter import filedialog as fd 
from tkinter import messagebox as ms 
from PIL import ImageTk, Image 

#  Build A Image Viewer Now
 
class Image_Viewer: 
    
    def __init__(self,master): 
        self.master = master 
        self.c_size = (700,500) 
        self.setup_gui(self.c_size) 
        self.img=None 
    
    def setup_gui(self,s): 
        Label(self.master,text='Image Viewer',pady=5,bg='white', font=('Arial',30)).pack() 
        self.canvas = Canvas(self.master,height=s[1],width=s[0], bg='Black',bd=10,relief='ridge')
        self.canvas.pack() 
        txt = '''
                                     By Shrimad Mishra 
                                          on behaf of 
                                              CodeSpeedy
                                ''' 
        self.wt = self.canvas.create_text(s[0]/2-270,s[1]/2,text=txt ,font=('',30),fill='white') 
        f=Frame(self.master,bg='white',padx=10,pady=10) 
        Button(f,text='Open Image',bd=2,fg='white',bg='black',font=('',15) ,command=self.make_image).pack(side=LEFT) 
        f.pack() 

    def make_image(self):   
        
        try: 
            File = fd.askopenfilename() 
            self.pilImage = Image.open(File) 
            re=self.pilImage.resize((700,500),Image.ANTIALIAS) 
            self.img = ImageTk.PhotoImage(re) 
            self.canvas.delete(ALL) 
            self.canvas.create_image(self.c_size[0]/2+10,self.c_size[1]/2+10, anchor=CENTER,image=self.img) 
            self.status['text']='Current Image:'+File 
        
        except: 
            ms.showerror('Error!','File type is unsupported.') 

root=Tk() 
root.configure(bg='white') 
root.title('Image Viewer') 
Image_Viewer(root) 
root.resizable(0,0) 
root.mainloop()

We got an awesome output to check it:-

Leave a Reply

Your email address will not be published. Required fields are marked *