Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Q1-creating an image of zeroes and show the image

import numpy as np
from PIL import Image
import cv2
import imageio
import math
plt.imshow(poly)
x=np.zeros([28,28,3],dtype=np.uint8) #an array which contain all pixels with zero
value

I= Image.fromarray(x) #converting the array into image

#cv2.imshow('first_image',I)

I.save('/home/ravi/Documents/Assignmntnjddsofjdjofjodjfdjo/first.png') #saving a
photo of black background

q1 = cv2.imread('/home/ravi/Documents/Assignmntnjddsofjdjofjodjfdjo/first.png')
#loading the image

cv2.imshow('first_image',q1) #displaying the image

cv2.waitKey(0) # waits until a key is pressed


cv2.destroyAllWindows() # destroys the window showing image
#plt.imshow(q1)

###########################################################################

#Q2-square image of black background and white square in it


import numpy as np
from PIL import Image
import cv2
import imageio
import math

from PIL import Image#for image manipulation

second_im = Image.new( 'RGB', (250,250), "black") #black background image


white_sqr = second_im.load() #loading pixels

for i in range(50,200):
for j in range(50,200):
white_sqr[i,j] = (255, 255, 255) #setting the pixel as white

second_im.show()
cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image

###################################################################################
##############

#Question4-accessing individual pixel and replace the value of it


##importing libraries
import cv2
import numpy as np
from PIL import Image#for image manipulation
s= cv2.imread('/home/ravi/Documents/Assignmntnjddsofjdjofjodjfdjo/first.png')
#loading the image
print("pixel value before is:",s[12,12]) #pixel value before
s[12,12]=(100,100,100) #setting the pixel at 12,12 as 100,100,100
print("pixel value after manipulation is:",s[12,12])

###################################################################################
#######################
##q8-Draw-
#1. Diagonal blue line with thickness of 6 pixels
#A ractange 3 An ellipse A polygon
import numpy as np
from PIL import Image
import cv2
import imageio
import math
x=np.zeros([28,28,3],dtype=np.uint8) #creating the background

#####drawing a diagonal with width=6


starting_pt=(0,0)
end_pt=(27,27)
nameofimg='fotu'
color=(0,0,255)
width=6
diag=cv2.line(x,starting_pt,end_pt,color,width) #using cv2.line command
plt.imshow(diag)

cv2.waitKey(0)
cv2.destroyAllWindows()

##drawing an ellipse
import cv2
from matplotlib import pyplot as plt
backgr = np.ones((200,200,3)) #a 200*200 pixel image with 3 channels

centre_x,centre_y = 99,99 #center of ellipse

major_ax,minor_ax = 40,40 #length of major ad minor axes

angL = 0 #inclination

centreofell = (centre_x,centre_y)

axes = (major_ax,minor_ax)

cv2.ellipse(backgr, centreofell, axes, angL, 0 , 360, (255,0,0), 2)

plt.imshow(backgr) #plotting the ellipse


#####drawing an rectange

rect = np.zeros((255,255,3), np.uint8)


rect = cv2.rectangle(rect,(125,20),(250,250),(0,0,255),3)
plt.imshow(rect)

#####drawing a polygon

backr_poly = np.ones((100,100,3), np.uint8)


pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)

poly = cv2.polylines(backr_poly,[pts],True,(0,255,255))
plt.imshow(poly)

You might also like