Skip to content

Image Analytics With Python – Creating Basic Photoshop App

This article explains about image  analysis with python by creating a basic photoshop app. The activity below gives a clear idea on reading image files , plotting them & editing them adding convolutions. 

Following activity will be a basic image editor which helps make an image black and white , blur , detect edges etc.

What is an Image ?

Image can be defined as a 2/3 -dimensional representation which is a visual display of something.
Example :  your selfie , x-ray etc
Image is 2-dimensional if its black & white and  3-dimensional if its coloured.

Smallest unit of an image is called pixel.

Few of famous image formats include TIFF , JPEG,GIF , PNG  etc.

What are different types of image ?

blackNWhite

Black and White Images

Binary images or Black and white images  are whose pixels have only two visible colours. Numerically, the two visible values are often 0 for black, and either 1 or 255 for white.

Grey Scale Images

Grey scale image contains only shades of grey and no colour. These are very similar to black and white images with a tint of grey in them. 
GreyScale
image_3

Colour Images

A colour image has three values (or channels) per pixel.  Every colour image is made from combination of any of  RED , GREEN & BLUE.

A colour image is  3D unlike B&W and Grey Scale which are 2D !

 

 

 

Why do we need image analytics?

With huge buzz around Computer Vision, Image form base of any analytics of graphics or videos. We should remember videos are nothing but  moving images. 

Few of real word applications of image analytics include Google image search, Facebook auto tagging etc

How can we do image analytics?

In simple terms , every image we see is collection of pixels from the backend. To edit any images we need to play with its pixels. Be it removing few or adding some. 

We convert images to matrix of pixel values and then continue our analytics journey

 

pixels

 How does a computer see an image ? 

As we see , the image is interpreted as a matrix where each pixel has a specific value.  Once the image is converted to numbers , phew ! We know how can we do analytics. 

what computers see

Before we begin !
Following program will give an overview on how an image processing /Analytics can be carried on. 

Python VersionDifficulty LevelPre-Requisites 
2.7+EasyBasic Python
Download the input image from THIS LINK
”’
import necessary libraries
if you get “No Module found” errror; Try instaalling using : pip install <missing module name>
”’

 

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

 

from scipy.signal import convolve2d

 

%matplotlib inline
”’
load the 512 * 512 image
”’
pic = mpimg.imread(‘prretty.png’)
#how does it look
plt.imshow(pic)
pic.shape
image_1

Make an image black  & white

”’
A coloured image has 3 axis. We only retain 2 by applying mean on axis = 2
”’
blackNwhite = img.mean(axis=2)
plt.imshow(blackNwhite, cmap=‘gray’)
image_2

Lets blur an image !

”’
‘create a Gaussian filter (GF)
”’
GF = np.zeros((20, 20))
for i in range(20):
for j in range(20):
dist = (i – 9.5)**2 + (j – 9.5)**2 #play around with values
GF[i, j] = np.exp(-dist / 50.)
GF /= GF.sum() # normalize the kernel

#view the GF image
plt.imshow(GF, cmap=‘gray’)
”’
Let’s add a convolution GF to blackNwhite image
”’
out = convolve2d(blackNwhite, GF)
plt.imshow(out, cmap=‘gray’)
image_5
”’
we see current image is not of shape 512*512 (as of our original image)
After convolution, the output shape will be modified to N1 + N2 – 1
”’
out.shape
(531, 531)
”’
using mode = ‘same’ keeps image size intact
”’
out = convolve2d(blackNwhite, GF, mode=‘same’)
plt.imshow(out, cmap=‘gray’)
image_5
”’
We blur the coloured image
”’
out3 = np.zeros(pic.shape)
print(out3.shape)
for i in range(3):
out3[:,:,i] = convolve2d(img[:,:,i], W, mode=‘same’)
plt.imshow(out3)
image_3

Lets detect edges !

”’
approximate gradient in X dir
We delare a matrix with standard values # feel free to play arround with numbers
”’
Hx = np.array([
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1],
], dtype=np.float32)

Hy = Hx.T
”’
Let’s add a convolution Hx to blackNwhite image
”’

Gx = convolve2d(blackNwhite, Hx)
plt.imshow(Gx, cmap=‘gray’)
blur_1
”’
Let’s add a convolution Hy to blackNwhite image
”’

Gy = convolve2d(blackNwhite, Hy)
plt.imshow(Gy, cmap=‘gray’)
blur_2

Wow , We developed a basic image editor in just few lines of code !

You may love this recent posts  !