Working With Images: Student Name Student Roll # Program Section
Working With Images: Student Name Student Roll # Program Section
Student Roll #
Program
Section
___________________________________________________________________________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
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
# 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)
___________________________________________________________________________35
Artificial Intelligence-Lab [COSC-3212]
2. Bar Plot
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
3. Histogram
# Y-axis values
y = [10, 5, 8, 4, 2]
___________________________________________________________________________36
Artificial Intelligence-Lab [COSC-3212]
# Function to show the plot
plt.show()
Output
4. Scatter Plot
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
___________________________________________________________________________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.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
___________________________________________________________________________39
Artificial Intelligence-Lab [COSC-3212]
2. Using MatplotLib
# 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()
Output
___________________________________________________________________________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
___________________________________________________________________________42
Artificial Intelligence-Lab [COSC-3212]
SAVING IMAGE IN PYTHON
import matplotlib.pyplot as plt
import matplotlib.image as img
plt.show()
___________________________________________________________________________43
Artificial Intelligence-Lab [COSC-3212]