1 of 17

Image Processing�Python

Satishkumar L. Varma

Professor, Department of Computer Engineering

PCE, New Panvel

www.sites.google.com/site/vsat2k

2 of 17

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Image Processing Library in Python
  • Install Library
  • Read Image
  • Write Image
  • Operations on Image
  • Image Processing Using GUI

2

3 of 17

Image Processing Library in Python

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Pillow / PIL
  • scikit-image
  • scipy
  • Mahotas
  • PythonMagick
  • pycairo
  • OpenCV-Python
  • SimpleITK

3

4 of 17

Install Library

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Install using cmd Terminal

pip install Pillow

  • Import in Python Code

from PIL import Image

4

5 of 17

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

6 of 17

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

7 of 17

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

8 of 17

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

9 of 17

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

10 of 17

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

11 of 17

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

11

12 of 17

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

12

13 of 17

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

13

14 of 17

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

14

15 of 17

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

15

16 of 17

References

16

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k www.sites.google.com/site/vsat2k

  1. Zed A. Shaw, “Learn Python the Hard Way”, Third Edition, Addison-Wesley, 2014.
  2. Jan Erik Solem, “Programming Computer Vision with Python”, Creative Commons.
  3. Fredrik Lundh, Matthew Ellis, “Python Imaging Library Overview”, Manning Publications, 2002.

17 of 17

Thank You.

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k www.sites.google.com/site/vsat2k

17