How To Create Image Viewer Using Python, Tkinter And PIL/pillow

Posted by Suraj Singh on February 19, 2017 · 15 mins read
hii readers,
                               

                                                             Today's Tutorial is about how to create image viewer using python tkinter and pil/pillow image library or you can say how to use pil/pillow with python Tkinter canvas widget.

so, let's start but first if you are new visitor then don't forget to check our index page because there you will find really very interesting stuff.

hmm, 




                                                          In Today's Topic we will use python PIL/pillow Image Library. because with this module we can do many interesting works with images. hence, for today's topic first install pil/pillow in you pc.

Here, For This Purpose You Need To Create Four Scripts.

1. Config.py (For All Configurations)

2. Path.py     (For Path Related Functions)

3. GUI.py     (For Graphic User InterFace Functions)

4. ImageHandle.py (For Handling Image Functions)

Here, I am Dividing All Works In 4 Scripts because with this division we can do our work more easily.

let me explain what we will do with this four script.

here, we will use config.py as all configurations settings file. because with this file we can do change easily. 
Path.py is for handling Image Directory Path And Image Path. 
GUI.py is for handling Gui Control Functions And Widget Looks. And at the end,  ImageHandle.py this script is for handling image functions like image size, etc

so, let's see practical demo codes. 
(Note : Paste all Scipts in same folder)

 Here, I am Sharing My Demo Codes But If You Want More Better Example Then, You Can Modify these codes yourself or Download This Script From My GitHub repository (link given at the end of these codes ).

1. Config.py

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python

# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This Script Is Created For http://www.bitforestinfo.com
# This Script is Written By
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''
# Configurations
# Enter Path Of Image Directories
ImageDir = ["/home/hackwithssb/Pictures/Wallpapers",
"/home/hackwithssb/Pictures",
"/home/hackwithssb/Pictures/BingWallpapers"]

# Image Extensions Allowed
Extension = ['JPG','BMP','PNG']

2. path.py


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python
# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This Script Is Created For http://www.bitforestinfo.com
# This Script is Written By
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''
import os
from config import *

def get_list():
Images=[]
for ImageP in ImageDir:
for i in os.listdir(ImageP):
Image = os.path.join(ImageP,i)
ext = Image.split('.')[::-1][0].upper()
if ext in Extension:
Images.append(Image)
return Images

3. ImgHandle.py

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/python

# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This Script Is Created For http://www.bitforestinfo.com
# This Script is Written By
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''
from PIL import Image, ImageTk

def tk_image(path,w,h):
img = Image.open(path)
img = img.resize((w,h))
storeobj = ImageTk.PhotoImage(img)
return storeobj

4. Gui.py


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/python

# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This Script Is Created For http://www.bitforestinfo.com
# This Script is Written By
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''
# Import Module
try:
import Tkinter
except:
import tkinter as Tkinter
import path
import ImgHandle

# Creating Canvas Widget
class PictureWindow(Tkinter.Canvas):
def __init__(self, *args, **kwargs):
Tkinter.Canvas.__init__(self, *args, **kwargs)
self.imagelist = path.get_list()
self.imagelist_p=[]
self.all_function_trigger()

def show_image(self, path):
img=ImgHandle.tk_image(path,self.winfo_screenwidth(),self.winfo_screenheight())
self.delete(self.find_withtag("bacl"))
self.allready=self.create_image(self.winfo_screenwidth()/2,self.winfo_screenheight()/2,image=img, anchor='center', tag="bacl")

self.image=img
print self.find_withtag("bacl")
self.master.title("Image Viewer ({})".format(path))
return

def previous_image(self):
try:
pop = self.imagelist_p.pop()
self.show_image(pop)
self.imagelist.append(pop)
except:
pass
return

def next_image(self):
try:
pop = self.imagelist.pop()

self.show_image(pop)
self.imagelist_p.append(pop)
except EOFError as e:
pass
return

def all_function_trigger(self):
self.create_buttons()
self.window_settings()
return

def window_settings(self):
self['width']=self.winfo_screenwidth()
self['height']=self.winfo_screenheight()
return

def create_buttons(self):
Tkinter.Button(self, text=" > ", command=self.next_image).place(x=(self.winfo_screenwidth()/1.1),y=(self.winfo_screenheight()/2))
Tkinter.Button(self, text=" < ", command=self.previous_image).place(x=20,y=(self.winfo_screenheight()/2))
self['bg']="white"
return

# Main Function
def main():
# Creating Window
root = Tkinter.Tk(className=" Image Viewer")
# Creating Canvas Widget
PictureWindow(root).pack(expand="yes",fill="both")
# Not Resizable
root.resizable(width=0,height=0)
# Window Mainloop
root.mainloop()
return

# Main Function Trigger
if __name__ == '__main__':
main()

For Downloading Raw Scripts Click Here.

For Live Example Check This Video
For More Update, Visit Our Regularly. 
And Subscribe Our Blog, 

Follow Us and share it.
For Any Type of Suggestion Or Help
Contact me:
Suraj
surajsinghbisht054@gmail.com