Python - Simple Image Viewer

In this tutorial we will create a Simple Image Viewer using Python. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. So let do the coding.

Getting started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Importing Modules

After setting up the installation, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor
  1. from tkinter import *
  2. from PIL import Image, ImageTk

Creating the Interface

This is the interface code for the Python application.The window will automatically generate when the program run. To do that just copy the code below then paste it inside the IDLE text editor.
  1. root = Tk()
  2. root.title("Python: Simple Image Viewer")
  3.  
  4. width = 600
  5. height = 300
  6. screen_width = root.winfo_screenwidth()
  7. screen_height = root.winfo_screenheight()
  8. x = (screen_width/2) - (width/2)
  9. y = (screen_height/2) - (height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. root.resizable(0, 0)

Creating The Main Function

This is the Main Code for the Python application. The code contains class that has a several function that will be called. To do that just copy the code below then paste it inside the IDLE text editor.
  1. #================================METHODS========================================    
  2. def DisplayImage(event=None):
  3.     Home = Toplevel()
  4.     Home.title("Simple Image Viewer/Viewer")
  5.     width = 800
  6.     height = 600
  7.     screen_width = Home.winfo_screenwidth()
  8.     screen_height = Home.winfo_screenheight()
  9.     x = (screen_width/2) - (width/2)
  10.     y = (screen_height/2) - (height/2)
  11.     Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
  12.     Home.resizable(0, 0)
  13.     load = Image.open('images/flower.jpg')
  14.     load = load.resize((width, height), Image.ANTIALIAS)
  15.     render = ImageTk.PhotoImage(load)
  16.     panel = Label(Home, image=render)
  17.     panel.image=render
  18.     panel.pack(fill=BOTH, expand=YES)
  19.  
  20.  
  21. #================================FRAMES=========================================
  22. Top = Frame(root, width=600, bd=1, relief=SOLID)
  23. Top.pack(side=TOP)
  24. Mid = Frame(root, width=300, height=200, bd=1, relief=SOLID)
  25. Mid.pack_propagate(0)
  26. Mid.pack(pady=20)
  27.  
  28.  
  29. #================================LABEL WIDGETS==================================
  30. lbl_title = Label(Top, text="Python: Simple Image Viewer", width=600, font=("arial", 20))
  31. lbl_title.pack(fill=X)
  32. lbl_text = Label(Mid, text="flower.jpg", font=("arial", 20))
  33. lbl_text.bind("<Button-1>", DisplayImage)
  34. lbl_text.pack()

Initializing the Application

This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
  1. #================================INITIALIZATION=================================
  2. if __name__ == '__main__':
  3.     root.mainloop()
There you have it we just created a Simple Image Viewer Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Tags

Add new comment