0% found this document useful (0 votes)
38 views6 pages

"Lab Manual (Lab 4) ": "Artificial Intelligence"

This lab manual document describes two tasks for an artificial intelligence lab: 1) Converting a colored image to grayscale using OpenCV by loading the image, converting it to grayscale, and displaying the original and grayscale images. 2) Dividing an image into four parts by slicing the image height into parts of a given slice size, cropping the image bounds for each slice, saving as new images. The code provided slices an input image into parts and saves them.

Uploaded by

Hafsa Quddus 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

"Lab Manual (Lab 4) ": "Artificial Intelligence"

This lab manual document describes two tasks for an artificial intelligence lab: 1) Converting a colored image to grayscale using OpenCV by loading the image, converting it to grayscale, and displaying the original and grayscale images. 2) Dividing an image into four parts by slicing the image height into parts of a given slice size, cropping the image bounds for each slice, saving as new images. The code provided slices an input image into parts and saves them.

Uploaded by

Hafsa Quddus 011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

“Lab Manual (lab 4)”

“Artificial Intelligence”
Lab 04
Artificial Intelligence
Task 1

Covert the coloured image into grey scale Image


# importing opencv

import cv2

# Loading the input image

image = cv2.imread(r"C:\Users\Hafsa\Desktop\A.jpg")

cv2.imshow('Original', image)

cv2.waitKey()

# using cvtColor function, to convert real image to grayscale

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow('Grayscale', gray_image)

cv2.waitKey(0)

# window shown waits for any key pressing event

cv2.destroyAllWindows()

Output
Orginal

Gray Scale
Task 2
Convert image into four parts.
# -*- coding: utf-8 -*-

@author: Hafsa

"""

from __future__ import division

from PIL import Image

import math

import os

def long_slice(image_path, out_name, outdir, slice_size):

"""slice an image into parts slice_size tall"""

img = Image.open(image_path)

width, height = img.size

upper = 0

left = 0

slices = int(math.ceil(height/slice_size))

count = 1

for slice in range(slices):

#if we are at the end, set the lower bound to be the bottom of the image
if count == slices:

lower = height

else:

lower = int(count * slice_size)

bbox = (left, upper, width, lower)

working_slice = img.crop(bbox)

upper += slice_size

#save the slice

working_slice.save(os.path.join(outdir, "slice_" + out_name + "_" + str(count)+".png"))

count +=1

if __name__ == '__main__':

long_slice(r"C:\Users\Hafsa\Desktop//A.jpg","test", os.getcwd(), 50)

long_slice(r"C:\Users\Hafsa\Desktop//A.jpg","test", os.getcwd(), 50)

long_slice(r"C:\Users\Hafsa\Desktop//A.jpg","test", os.getcwd(), 50)

long_slice(r"C:\Users\Hafsa\Desktop//A.jpg","test", os.getcwd(), 50)

Output

You might also like