Hi Python Computer Vision— PIL!

An Intro To Python Imaging Library #PyVisionSeries — Episode #00

J3
Jungletronics
5 min readOct 4, 2020

--

Python support for opening, manipulating, and saving many different image file formats.

Let’s get started with Python Computer Vision!

print(‘Hello Python Computer Vision!’)Hello Python Computer Vision!

Python image libraries, like many others, are on top of the NumPy library and also depend on matplotlib too. Go ahead and import them now!

import numpy as np
import matplotlib.pyplot as ptl

In order to ensure that the matplotlib lib runs on a single line add this piece of code as well:

%matplotlib inline

Now, Pillow!

Pillow is the friendly PIL fork by Alex Clark and Contributors.

PIL is the Python Imaging Library by Fredrik Lundh and Contributors.

from PIL import Image

In order to get started let’s import any image .jpg (you can download the image from my google drive link 00_puppy.jpg:)

Here I’ll be using Google Colab (colab.research.google.com)!

Open a new project and click on hamburger icon > Files > Upload to Section Storage the 00_puppy.jpg (or any you’d like):

pic = Image.open(‘00_puppy.jpg’)

Let’s see what type of image this is:

type(pic)PIL.JpegImagePlugin.JpegImageFile

To work with an image, let’s cast it to NumPy array:

pic_arr = np.asarray(pic)type(pic_arr)numpy.ndarray

Let’s see the Pixel resolution.

The term resolution is often considered equivalent to pixel count in digital imaging.

pic_arr.shape(551, 789, 3)

Here Python is telling us that the image is 551 x 789 long and there are 3 channels available: Red, Green, and Blue (RGB).

So far, so Good!

Let`s see the picture.

ptl.imshow(pic_arr)
Fig 1. Note that when transforming into a NumPy array, we map the image automatically. Now each pixel is on our command! Perfect!

I think you’ve heard about screen printing.

The Silkscreen is the technique that consists of separating the screens in primary colors to print them one-by-one.

Fig 2. From Art Silkscreen Stock Illustrations — 546 Art Silkscreen Stock Illustrations, Vectors & Clipart — Dreamstime

We will apply the same principle here.

Let’s separate the three channels from the puppy image above and see what happens.

Let’s begin by creating a copy of our original image:

pic_copy = pic_arr.copy()

Now let’s filter only the Red Channel Values(R=0, G=1, B=2):

# *R G B
# RED CHANNEL VALUES (0–255)
ptl.imshow(pic_copy[:,:,0], cmap=’gray’)
Fig 3. We are just mapping the image to show gray. If you were using screen printing techniques (silkscreen) you would probably use this filtered image to apply the red color (see that in carrots, the red color will be used a lot…)

Up to here we just select the RED channel.

The Python Slicing techniques can be learned in this very simple post.

Now repeat the same procedures to filters out the other two colors.

For Green (1):

# R *G B
# GREEN CHANNEL VALUES(0–255)
ptl.imshow(pic_copy[:,:,1], cmap=’gray’)
Fig 4. See that the green is painted on the grass, with a little more intensity on the puppy’s hind legs …

For Blue (2):

# R G *B
# BLUE CHANNEL VALUES(0–255)
ptl.imshow(pic_copy[:,:,2], cmap=’gray’)
Fig 5. We painted the entire iron structure behind the puppy in pure blue

This last image is very illustrative: when we apply blue, see that in the steel structure behind the puppy there is a good amount of blue paint (thinking of the silkscreen technique).

In compensation, in the grass, in which green predominates, it does not pass not much blue. Got it?

I found it all really incredible in Python. Just a few lines of code and the sea opens… don't you think so, Captain?

Now let’s apply a technique to isolate only the red:

# Got rid of Green Channel
pic_copy[:,:,1] = 0
ptl.imshow(pic_copy)
Fig 6. Two colors predominate here: red and blue; the green vanished!
# Got rid of Blue Channel
pic_copy[:,:,2] = 0
ptl.imshow(pic_copy)
Fig 7. Now only the red color.

The pic_copy still has the info about all 3 channels except cleverly we went ahead and zeroed G & B channels!

ic_copy.shape
# Look!
pic_copy
(551, 789, 3)

And That’s it!

print(“That’s it! Python support for opening, manipulating, and saving many different image file formats. See you in the next PyVisionSeries Episode!”)That's it! Python support for opening, manipulating, and saving many different image file formats. See you in the next PyVisionSeries Episode!

See the deforestation of the Amazon!

Shall we understand more about how to work with satellite images?

Fig 8. Please, let’s protect the Amazon Rain Forest for future generations! This is a paradise in terms of biodiversity! I lived in Rondonia, and you can clearly see the destructive trail of highway BR-364 (center-left)…

Be tuned for the next episode of #PyVisionSeries!
See you soon!

Bye o/

Drive Link

Credits & References:

Jose Portilla — Python for Computer Vision with OpenCV and Deep Learning — Learn the latest techniques in computer vision with Python, OpenCV, and Deep Learning!

https://pillow.readthedocs.io/en/stable/

https://en.wikipedia.org/wiki/Image_resolution

https://en.wikipedia.org/wiki/Computer_vision

Posts Related:

00 Episode#Hi Python Computer Vision — PIL! — An Intro To Python Imaging Library #PyVisionSeries

01 Episode# Jupyter-lab — Python — OpenCV — Image Processing Exercises #PyVisionSeries

02 Episode# OpenCV — Image Basics — Create Image From Scratch #PyVisionSeries

03 Episode# OpenCV — Morphological Operations — How To Erode, Dilate, Edge Detect w/ Gradient #PyVisionSeries

04 Episode# OpenCV — Histogram Equalization — HOW TO Equalize Histograms Of Images — #PyVisionSeries

05 Episode# OpenCV — OpenCV — Resize an Image — How To Resize Without Distortion

07 Episode# YOLO — Object Detection — The state of the art in object detection Framework!

08 Episode# OpenCV — HaashCascate — Object Detection — Viola–Jones object detection framework — #PyVisionSeries

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!