Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
242 views

Digital Image Processing Lab Manual# 1

This document provides an introduction to Python programming by covering various Python concepts like variables, data types, operators, conditional statements, loops, functions, and working with images using OpenCV. It discusses how to define and assign variables, perform arithmetic operations, use conditional statements like if-else and while loops, define and call functions, read and write images, and work with lists and slicing. Key concepts covered include using print() to output values, basic data types like integers, floats and strings, operators, boolean logic, basic list operations, functions with parameters and return values, and reading/displaying images using OpenCV.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
242 views

Digital Image Processing Lab Manual# 1

This document provides an introduction to Python programming by covering various Python concepts like variables, data types, operators, conditional statements, loops, functions, and working with images using OpenCV. It discusses how to define and assign variables, perform arithmetic operations, use conditional statements like if-else and while loops, define and call functions, read and write images, and work with lists and slicing. Key concepts covered include using print() to output values, basic data types like integers, floats and strings, operators, boolean logic, basic list operations, functions with parameters and return values, and reading/displaying images using OpenCV.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB-01:Introduction to Python

OBJECTIVE:

To introduce students with python programming language.

1. # is used for commenting


# This is a comment.
2. Whatever you put inside parentheses of print function will be your output
print (5)
print (10)
print (12+6)
note: The print function automatically adds the new line character at the end
print (5, end=” ”)
print (“END”)
3. Operators
i. + plus
ii. - minus
iii. * multiply
iv. ** power
v. / divide
vi. // divide and floor
vii. % modulus
4. Variables
x=5
y, z = 3.14, 17.2 # right side is evaluated first and then assigned to left
side with corresponding values
print (x + y)
print(type(x)) # type () function returns the variable’s data type.
print(type(y))
z = True # here z is of class bool. True or False (first letter
capital)
The answer of equation containing one float is a float. The answer of / (divide)
operator is always float.

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)

10. The while statements


number = 23
while number<50:
print(number)
number+=1
else:
print ("wow there is an else for while statement as well")

11. The for statement


#Indentation
for i in range (10):
if i==6:
break
print(i)
for i in range (3, 10):
print(i)
if i==6:
continue
for i in range (1, 10, 3):
print(i)

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.

cv2.IMREAD_COLOR: It specifies to load a color image. Any


transparency of image will be neglected. It is the default flag.
Alternatively, we can pass integer value 1 for this flag.
cv2.IMREAD_GRAYSCALE: It specifies to load an image in grayscale
mode. Alternatively, we can pass integer value 0 for this flag.

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)

# Creating GUI window to display an image on screen


# first Parameter is windows title (should be in string format)
# Second Parameter is image array
cv2.imshow("image", img)

# To hold the window on screen, we use cv2.waitKey method


# Once it detected the close input, it will release the control
# To the next line
# First Parameter is for holding screen for specified milliseconds
# It should be positive integer. If 0 pass an parameter, then it will
# hold the screen until user close it.
cv2.waitKey(0)
 
# It is for removing/deleting created GUI window from screen
# and memory
cv2.destroyAllWindows()

cv2.imwrite() :method is used to save an image to any storage device. This


will save the image according to the specified format in current working
directory.
Syntax: cv2.imwrite(filename, image)
Parameters:
filename: A string representing the file name. The filename must include
image format like .jpg, .png, etc.
image: It is the image that is to be saved.
Return Value: It returns true if image is saved successfully.

# 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

You might also like