Image Processing�Python
Satishkumar L. Varma
Professor, Department of Computer Engineering
PCE, New Panvel
www.sites.google.com/site/vsat2k
Outline
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
2
Image Processing Library in Python
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
3
Install Library
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
pip install Pillow
from PIL import Image
4
Read Image
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
from PIL import Image
#im = Image.open('satish.png')
fp = open("satish.png", "rb")
im = Image.open(fp)
im.show()
print('Image Format: ', im.format)
print("Size of Image WxH: %dx%d" % im.size)
print('Color Model: ', im.mode)
5
Write Image
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
from PIL import Image
#im = Image.open('satish.png')
fp = open("satish.png", "rb")
im = Image.open(fp)
im.show()
print('Image Format: ', im.format)
print("Size of Image WxH: %dx%d" % im.size)
print('Color Model: ', im.mode)
im.save('satish_copy.png','PNG')
6
Image Thresholding
from PIL import Image
#im = Image.open('satish.png')
fp = open("satish.png", "rb")
im = Image.open(fp)
im.show()
print('Image Format: ', im.format)
print("Size of Image WxH: %dx%d" % im.size)
print('Color Model: ', im.mode)
th = 100
im = im.point(lambda p: p > th and 255)
im.save("satish_color_threshold100.jpg")
myGreyscale = im.convert('L')
myGreyscaleTh = myGreyscale.point(lambda p: p > th and 255)
myGreyscaleTh.save('satish_greyscale_threshold.jpg','JPEG')
7
Operations on Image
from PIL import Image
#im = Image.open('satish.png')
fp = open("satish.png", "rb")
im = Image.open(fp)
im.show()
print('Image Format: ', im.format)
print("Size of Image WxH: %dx%d" % im.size)
print('Color Model: ', im.mode)
myResize = im.resize((128, 128))
myResize.save('satish_resize128.png','PNG')
myRotate = im.rotate(45) # degrees counter-clockwise
myRotate.save('satish_rotate45.png','PNG')
myGreyscale = im.convert('L')
myGreyscale.save('satish_greyscale.jpg','JPEG')
box = (10, 10, 120, 120) #(left, upper, right, lower)
myRegion = im.crop(box)
#myRegion.show()
myRegion.save('satish_region.png','PNG')
8
Image Processing Using GUI
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
import tkinter as tk
from tkinter import *
main = tk.Tk()
main.geometry('550x350')
main.title('Image Thresholding')
from PIL import Image, ImageTk
#im = Image.open('satish.png')
fp = open("satish.png", "rb")
im = Image.open(fp)
img1 = ImageTk.PhotoImage(Image.open(fp))
panel1 = tk.Label(main, text = 'Input Image', image = img1)
print('Image Format: ', im.format)
print("Size of Image WxH: %dx%d" % im.size)
print('Color Model: ', im.mode)
9
Image Processing Using GUI
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
th = 100
myGreyscale = im.convert('L')
myGreyscaleTh = myGreyscale.point(lambda p: p > th and 255)
myGreyscaleTh.save('satish_greyscale_th.jpg','JPEG')
fp1 = open("satish_greyscale_th.jpg", "rb")
img2 = ImageTk.PhotoImage(Image.open(fp1))
panel2 = tk.Label(main, text = 'Input Image', image = img2)
panel1.grid(row=0,column=0)
panel2.grid(row=0,column=1)
10
Outline
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
11
Outline
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
12
Outline
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
13
Outline
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
14
Outline
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
15
References
16
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k www.sites.google.com/site/vsat2k
Thank You.
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k www.sites.google.com/site/vsat2k
17