Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

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 ↓