You are on page 1of 10

Student Name

Student Roll #

Program

Section

Working with Images


Lab# 07

___________________________________________________________________________34
Artificial Intelligence-Lab [COSC-3212]
PLOTTING IN PYTHON

INSTALLATION

Windows, Linux and macOS distributions have matplotlib and most of its dependencies. Run the
following command to install matplotlib package

pip install matplotlib

BASIC PLOTS in matplotlib

Matplotlib comes with a wide variety of plots. Plots helps to understand trends, patterns, and to
make correlations. They’re typically instruments for reasoning about quantitative information.
Some of the sample plots are covered here.

1. Line Plot

# importing matplotlib module


from matplotlib import pyplot as plt

# x-axis values
x = [5, 2, 9, 4, 7]

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot
plt.plot(x,y)

# function to show the plot


plt.show()
Output

___________________________________________________________________________35
Artificial Intelligence-Lab [COSC-3212]
2. Bar Plot

# importing matplotlib module


from matplotlib import pyplot as plt

# x-axis values
x = [5, 2, 9, 4, 7]

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot the bar


plt.bar(x,y)

# function to show the plot


plt.show()
Output

3. Histogram

# importing matplotlib module


from matplotlib import pyplot as plt

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot histogram


plt.hist(y)

___________________________________________________________________________36
Artificial Intelligence-Lab [COSC-3212]
# Function to show the plot
plt.show()
Output

4. Scatter Plot

# importing matplotlib module


from matplotlib import pyplot as plt

# x-axis values
x = [5, 2, 9, 4, 7]

# Y-axis values
y = [10, 5, 8, 4, 2]

# Function to plot scatter


plt.scatter(x, y)

# function to show the plot


plt.show()
Output

___________________________________________________________________________37
Artificial Intelligence-Lab [COSC-3212]
LABELING IN PYTHON
import matplotlib.pyplot as plt

x = [0, 2, 4, 6]

y = [1, 3, 4, 8]

plt.plot(x,y)

plt.xlabel('x values')

plt.ylabel('y values')

plt.title('plotted x and y values')

plt.legend(['line 1'])

Output

___________________________________________________________________________38
Artificial Intelligence-Lab [COSC-3212]
READING IMAGES in PYTHON

Python supports very powerful tools when comes to image processing. This lab investigates how
to process the images using different libraries like OpenCV, Matplotlib, PIL etc.

1. Using OpenCv

OpenCV (Open Source Computer Vision) is a computer vision library that contains various
functions to perform operations on pictures or videos. It was originally developed by Intel but was
later maintained by Willow Garage and is now maintained by Itseez. This library is cross-platform
that is it is available on multiple programming languages such as Python, C++ etc.

import cv2

# Read RGB image


img = cv2.imread('g4g.png')

# Output img with window name as 'image'


cv2.imshow('image', img)

# Maintain output window until user presses a key


cv2.waitKey(0)

# Destroying present windows on screen


cv2.destroyAllWindows()
Output

___________________________________________________________________________39
Artificial Intelligence-Lab [COSC-3212]
2. Using MatplotLib

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a


multi-platform data visualization library built on NumPy arrays and designed to work with the
broader SciPy stack. It was introduced by John Hunter in the year 2002. Matplotlib comes with a
wide variety of plots. Plots helps to understand trends, patterns, and to make correlations. They’re
typically instruments for reasoning about quantitative information.

# Python program to read


# image using matplotlib

# importing matplotlib modules


import matplotlib.image as mp
import matplotlib.pyplot as plt

# Read Images
img = mp.imread('g4g.png')

# Output Images
plt.imshow(img)
plt.show()
Output

3. Using PIL

PIL is the Python Imaging Library which provides the python interpreter with image editing
capabilities. It was developed by Fredrik Lundh and several other contributors. Pillow is the
friendly PIL fork and an easy to use library developed by Alex Clark and other contributors.

___________________________________________________________________________40
Artificial Intelligence-Lab [COSC-3212]
# Python program to read
# Python program to read
# image using PIL module

# importing PIL
from PIL import Image

# Read image
img = Image.open('g4g.png')

# Output Images
img.show()

# prints format of image


print(img.format)

# prints mode of image


print(img.mode)

Output

WORKING WITH .png IMAGES USING PYTHON

Applying Pseudocolor to Image: Pseudocolor is useful for enhancing contrast of image.

# importing pyplot and image from matplotlib


import matplotlib.pyplot as plt
import matplotlib.image as img

# reading png image


im = img.imread('imR.png')

___________________________________________________________________________41
Artificial Intelligence-Lab [COSC-3212]
# applying pseudocolor
# default value of colormap is used.
lum = im[:, :, 0]

# show image
plt.imshow(lum)
plt.show()

Output

Code #2: We can provide another value to colormap with colorbar.


# importing pyplot and image from matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as img

# reading png image


im = img.imread('imR.png')
lum = im[:, :, 0]

# setting colormap as hot


plt.imshow(lum, cmap ='hot')
plt.colorbar()
plt.show()
Output

___________________________________________________________________________42
Artificial Intelligence-Lab [COSC-3212]
SAVING IMAGE IN PYTHON
import matplotlib.pyplot as plt
import matplotlib.image as img

# reading png image


im = img.imread('imR.png')

plt.savefig('plot.png', dpi=300, bbox_inches='tight')

plt.show()

___________________________________________________________________________43
Artificial Intelligence-Lab [COSC-3212]

You might also like