Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

Image Processing Libraries in Python
Latest   Machine Learning

Image Processing Libraries in Python

Last Updated on July 24, 2023 by Editorial Team

Author(s): Akula Hemanth Kumar

Originally published on Towards AI.

Making computer vision easy with Monk, low code Deep Learning tool and a unified wrapper for Computer Vision.

Reference: https://www.tessellateimaging.com/

Table of contents

  1. OpenCV
  2. Python Pillow
  3. Scikit Image
  4. Conversions
  5. Quiz

OpenCV

What is OpenCV?

  • Open-source computer vision and machine learning software.
  • More than 2500 optimized algorithms.
  • A BSD-licensed product.
  • More than 47 thousand people of the user community.
  • Downloads exceeding 18million.

Supported Image formats

  • Windows bitmaps- ‘.bmp’, ‘.dib’
  • JPEG files- ‘.jpeg’ , ‘.jpg’ , ‘.jpe’
  • JPEG 2000 files- ‘.jp2’
  • Portable Network Graphics- ‘.jp2’
  • WebP- ‘.webp’
  • TIFF files- ‘.tiff’, ‘.tif’

Loading an image in OpenCV

  • Import library
'''
1.Import OpenCV
2.cv2 class object
3.cv2 acts as a master object encompassing all the functions and global parameters used in OpenCV python
'''
import cv2
  • Load using cv2.imread
'''
1.cv2.imread
2.First param - image
3.Second param - will be discussed in the next section
'''
img = cv2.imread("imgs/chapter1/tessellate.png", -1)
  • Display Image
'''
1.To display opencv image use matplotlib
2.using inline it shows otput just below the cell
'''
from matplotlib import pyplot as plt
import cv2
import numpy as np
img = cv2.imread("imgs/chapter1/tessellate.png", -1)'''
1.OpenCV loads an image in BGR format
2.BGR format is Blue, Green and Red format
3.With the display scale is also printed
'''
img2 = img[:,:,::-1] # For channel alignment
plt.imshow(img2)
plt.show()
  • Writing an image to disc using OpenCV
'''
1.Saving image to disc
2.Loaded a png image and saved it back as jpeg
3.cv2.imwrite
'''
cv2.imwrite("imgs/chapter1/tessellate_opencv_saved.jpg", img)

Python Pillow

What is Python PIL

  • PIL is the Python Imaging Library.
  • A free library for the Python programming language.
  • Supports for opening, manipulating and saving many different image file formats.
  • Pillow offers several standard procedures for image manipulation. These include:

1. per-pixel manipulations

2.Masking and transparency handling.

3. Image filtering, such as blurring, contouring, smoothing or edge finding.

4. Image enhancing, such as sharpening, adjusting brightness, contrast or color.

5.Adding text to images and much more.

Supported Image formats

  • PPM
  • PNG
  • JPEG
  • GIF
  • TIFF
  • BMP

Loading an Image in PIL

  • Import Library
'''
1.Import PIL
2.PIL object
3.Image instance is responsible for loading and saving images like cv2 is for OpenCV
'''
import PIL
from PIL import Image
  • Load image using Image.Open function
'''
1.This is a lazy operation; this function identifies the file, but the file remains open and the
actual image data is not read from the file until you try to process the data.
2.Image.open
3.Argument - image path and name
'''
img = Image.open("imgs/chapter1/tessellate.png")
  • Display Image
'''
1. To display Pillow image using matplotlib
'''
from matplotlib import pyplot as plt
plt.imshow(img)
plt.show()
  • Writing an image to disc using PIL
'''
1.Notice the difference here
2.In opencv we did cv2.imwrite() whereas in PIL we didn't use Image.save.
3.We used img.save where img is an image variable not the main Image class object
'''
img.save("imgs/chapter1/tessellate_pillow_saved.png")

Scikit Image

What is Scikit-Image?

  • A collection of algorithms for image processing.
  • Available free of charge and free of restriction.
  • Designed to interoperate with the Python numerical and scientific libraries Numpy and SciPy.
  • Includes algorithms for segmentation, geometric transformations, color space manipulation, analysis, filtering, morphology, feature detection, etc.

Loading an image in Scikit-Image

  • Import Library
'''
1.Import scikit image
'''
import skimage
  • Loading Image using scikit.io.open function
'''
Reading image using scikit
'''
img = skimage.io.imread("imgs/chapter1/tessellate.png")
  • Display Image
'''
Display skimage data using matplotlib
'''
from matplotlib import pyplot as plt
plt.figure(figsize=(8, 8))
plt.imshow(img)
plt.show()
  • Writing an image to disc using Scikit-Image
'''
1.imsave
'''
skimage.io.imsave("imgs/chapter1/tessellate_skimage_saved.png", img)

Conversions

Why Conversion from one library to other?

  • All algorithms are not present in every library. Hence inter-library conversions are important.
  1. OpenCV ← → Numpy
'''
1.OpenCV <--> Numpy
2. No explicit conversion required here
'''
import cv2inp = cv2.imread("imgs/chapter1/img1.png", -1)
print("Input Type = {}".format(type(inp)))

Output

Input Type = <class 'NoneType'>

2. PIL ← → Numpy

'''
1.PIL <--> Numpy
'''
from PIL import Image
import numpy as np
inp = Image.open("imgs/chapter1/tessellate.png")
print("Input Type = {}".format(type(inp)))

Output

Input Type = <class 'PIL.PngImagePlugin.PngImageFile'>
  • PIL → Numpy
'''
1.Conversion PIL to Numpy
2.np.asarray -> numpy take it "as" "array"
'''
out = np.asarray(inp)
print("Converted Type = {}".format(type(out)))

Output

Converted Type = <class 'numpy.ndarray'>
  • Numpy → PIL
'''
1.Conversion Numpy to PIL
2.Image.fromarray -> Image take it "from" "array"
'''
re_out = Image.fromarray(np.uint8(out))
print("Re-Converted Type = {}".format(type(re_out)))

Output

Re-Converted Type = <class 'PIL.Image.Image'>

3. Scikit-Image ← → Numpy

'''
1.Scikit Image <--> Numpy
'''
import skimage
inp = skimage.io.imread("imgs/chapter1/tessellate.png")
print("Input Type = {}".format(type(inp)))
print("Scikit images are subclass of numpy ndarray")
print("All operations of numpy, scipy and opencv work on a scikit image")

Output

Input Type = <class 'imageio.core.util.Array'> 
Scikit images are subclass of numpy ndarray
All operations of numpy, scipy and opencv work on a scikit image

Quiz

  1. Why is OpenCV image displayed using plt.imshow(img[:,:,::-1]) and PIL image using plt.imshow(img)?
  2. What is the core object in scikit-image that stores all the functions?
  3. When OpenCV and Pillow images were displayed they appeared smaller than scikit image’s display. Find out the reason? (Hint: See display cells of scikit and pillow)
  4. While Opening OpenCV using cv2.imread, what value of its second parameter reads a colored image? (Hint: Options — [-1, 1, 0, 2])

Please feel free to share your answers through comments below

You can find the complete jupyter notebook on Github.

If you have any questions, you can reach Abhishek and Akash. Feel free to reach out to them.

Photo by Srilekha

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓