0% found this document useful (0 votes)
10 views10 pages

Apl Practical 2

The document describes an Advanced Programming Lab assignment to perform various operations on images using Python code. The operations include pasting one image on another, getting an image histogram, transposing an image, splitting an image into bands, converting an image to bitmap, and creating a thumbnail. Code snippets are provided to demonstrate how to perform each operation using Python imaging libraries like PIL and OpenCV.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Apl Practical 2

The document describes an Advanced Programming Lab assignment to perform various operations on images using Python code. The operations include pasting one image on another, getting an image histogram, transposing an image, splitting an image into bands, converting an image to bitmap, and creating a thumbnail. Code snippets are provided to demonstrate how to perform each operation using Python imaging libraries like PIL and OpenCV.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Name : Vishakha Sunil Patil

PRN Number : 2041064

Subject : Advanced Programming Lab

Aim : Write a program to perform following operations on images.

a. Pasting an image on another image


b. Getting a histogram of an image
c. Transposing an image
d. Split an image into individual bands
e. Convert image to bitmap
f. Creating a thumbnail

Program :

#Pasting an image on another image


from PIL import Image
img1 = Image.open("paste_image1.jpg")
img2 = Image.open("paste_image2.jpg")
Image.Image.paste(img1, img2, (50, 125))
img1.show()

Output :
Program :

#Getting a histogram of an image


import cv2
from matplotlib import pyplot as plt
img = cv2.imread('histogram2.jpg', 0)
plt.hist(img.ravel(), 256, [0, 256])
plt.show()
Output :
Program :

#Transposing an image
from PIL import Image
img = Image.open("transpose.jpg")
width, height = img.size
left = 6
top = height / 4
right = 174
bottom = 3 * height / 4
img1 = img.crop((left, top, right, bottom))
newsize = (200, 200)
img1 = img1.transpose(Image.FLIP_LEFT_RIGHT)
img1.show()

Output :
Program :

#Split an image into individual bands


from PIL import Image
img = Image.open("split.jpg")
img1 = Image.Image.split(img)
img1[0].show()
img1[1].show()
img1[2].show()
Output :
Program :

#Convert an image to bitmap


from PIL import Image
import numpy as np
img = Image.open("bitmap.jpg")
ary = np.array(img)
r, g, b = np.split(ary, 3, axis = 2)
r = r.reshape(-1)
g = r.reshape(-1)
b = r.reshape(-1)
bitmap = list(map(lambda x : 0.299 * x[0] + 0.587*x[1] + 0.114*x[2], zip(r, g,
b)))
bitmap = np.array(bitmap).reshape([ary.shape[0], ary.shape[1]])
bitmap = np.dot((bitmap > 128).astype(float), 255)
img = Image.fromarray(bitmap.astype(np.uint8))
img.show()

Output :
Program :

#Creating a thumbnail
from PIL import Image
img = Image.open("thumbnail.jpg")
MAX_SIZE = (100, 100)
img.thumbnail(MAX_SIZE)
img.show()
Output :

You might also like