Digital Image Processing Lab Manual# 1
Digital Image Processing Lab Manual# 1
OBJECTIVE:
Page 1 of 9
print (type (10/2))
5. Strings
print (“hello world”)
print (‘hello world’) #both single and double quotes works same
print (“hello ‘world’ ”)
print (“hello \”world\” ”) #escape character( \ )
a=”py”
b=’charm’
print (a + b) # + will join the two strings
print (a, b)
print (a*5)
print (a + 5) # Type Error: must be str, not int
print (a + str (5)) # str ( ) function converts int or float to string
type
6. LISTS
x = [1,2,3,4]
note: list are not arrays because arrays are of single data type however list may
contain variables of different data types.
x=int (input (“Enter a number”) # input ( ) function always input string,
# int ( ) function is used to convert string to int
y = 3.14
z = "HELLO"
li = [x, y, z, 4]
print(li)
List indexes
# From left to right: 0 1 2
# From right to left: -1 -2 -3
print (li [2] [-4])
slicing
#list [start: end: optional step size] ; start is inclusive and end is exclusive
print (li [1:3])
print (li [0:4:2])
print (li [:])
print (li [0:])
Page 2 of 9
print (li [:3])
print (li [2] [1:4])
#Deleting something from a list
del (li [2])
copy
w = [1, 2, 3, 4]
x=w # The assignment copies the reference to the original list
y = w [:] # Slicing creates a new list
x [0] = 6
y [1] = 9
print(w)
7. Indentation
Whitespace is important in Python. Actually, whitespace at the beginning of
the line is important. This is called indentation. Leading whitespace (spaces
and tabs) at the beginning of the logical line is used to determine the
indentation level of the logical line, which in turn is used to determine the
grouping of statements.
Try this.
x=2
if x==10:
print ("this is inside if block")
print ("this is also inside if block")
print("this will print always")
8. Boolean operations
< (less than)
(greater than)
<= (less than or equal to)
>= (greater than or equal to)
== (equal to)
! = (not equal to)
not (boolean NOT) if not x:
and (boolean AND) if x==2 and y>4:
or (boolean OR)
Page 3 of 9
9. The IF statement
number = 23
if number == 24:
print ('number is equal to 24')
elif number<24:
print (Number is less than 24)
else:
print (Number is higher than 24)
Page 4 of 9
12. Functions
def print_max (a, b=0):
if a > b:
print (a, 'is maximum')
elif a == b:
print (a, 'is equal to', b)
else:
print (b, 'is maximum')
print_max (3, 4)
x=5
y=7
print_max (x, y)
Keyword arguments
def func (a, b=5, c=10):
print ('a is', a, 'and b is', b, 'and c is', c)
func (3, 7)
func (25, c=24)
func (c=50, a=100)
Return
def max (x, y):
if x > y:
return x
else:
return y
m=max (3,5)
print(m)
Reading an Image:
Page 5 of 9
OpenCV (Open Source Computer Vision) is a computer vision library
that contains various functions to perform operations on pictures or
videos.
How to install:
https://medium.com/@pranav.keyboard/installing-opencv-for-python-
on-windows-using-anaconda-or-winpython-f24dd5c895eb
To read the images cv2.imread() method is used. This method loads
an image from the specified file. If the image cannot be read (because
of missing file, improper permissions, unsupported or invalid format)
then this method returns an empty matrix.
Syntax: cv2.imread(path, flag)
Parameters:
path: A string representing the path of the image to be read.
flag: It specifies the way in which image should be read. It’s default value
is cv2.IMREAD_COLOR
Return Value: This method returns an image that is loaded from the
specified file.
Page 6 of 9
# To read image from disk, we use
# cv2.imread function, in below method,
img = cv2.imread("lab1.png", cv2.IMREAD_COLOR)
# Image path
image_path = r'C:\Users\old...’
# Image directory
directory = r'C:\Users\new....
# Using cv2.imread() method
# to read the image
img = cv2.imread(image_path)
# Change the current directory
Page 7 of 9
# to specified directory
os.chdir(directory)
# List files and directories
# in 'C:/Users/old’
print("Before saving image:")
print(os.listdir(directory))
# Filename
filename = 'savedImage.jpg'
# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, img)
# List files and directories
# in 'C:/Users / new’
print("After saving image:")
print(os.listdir(directory))
print('Successfully saved')
TASK 1:
x = [[1, 2, 3, 4, 5], [21, 22, 23, 24, 25], [31, 32, 33, 34, 35]]
Write python code using python indexing and slicing for the following output.
Use only one print statement for each
i. [31, 32, 33, 34, 35]
ii. 23
iii. [22, 23]
iv. [1, 3, 5]
Declare y = [0, 0, 0], now using for loop write average of first list in list ‘x’ on
first index of list y and so on. The print(y) should give the following output
o [3.0, 23.0, 33.0]
Page 8 of 9
TASK 2:
x = [1, 3, 5, 6, 7, 8, 6, 1, 2, 3]
y = [0, 0, 0, 0, 0, 0, 0, 0]
Write python code using while loop that write average of first three items on
first index of y and so on. The print(y) should give the following output
o [3.0, 4.666666666666667, 6.0, 7.0, 7.0, 5.0, 3.0, 2.0]
Define a function that takes list length as argument and returns the average.
Then calculate the average of x and y.
Task#3
Save our university logo from website and read it in grey scale and as colour
image. Show it in GUI. Then save it in different directory
Conclusion:
This lab gives a basic introductory session on the programming using python language.
Page 9 of 9