In: Computer Science
1) Write a function to convert the image to grayscale.
`
original_pixels = [] def grayScale(pic): for p in getPixels(pic): r = int(getRed(p)) g = int(getGreen(p)) b = int(getBlue(p))//I have tried this with and without the int() original_pixels.append((r, g, b)) new = (r + g + b)/3 color= makeColor(new,new,new) setColor(p, color)
`
2) Write a function to convert an image to black and white
`
import cv2 import numpy as np img1 = cv2.imread('opencvlogo.png') row,col,ch = img1.shape g = [ ] #the list in which we will stuff single grayscale pixel value inplace of 3 RBG values #this function converts each RGB pixel value into single Grayscale pixel value and appends that value to list 'g' def rgb2gray(Img): global g row,col,CHANNEL = Img.shape for i in range(row) : for j in range(col): a = ( Img[i,j,0]*0.07 + Img[i,j,1]*0.72 + Img[i,j,2] *0.21 ) #the algorithm i used id , G = B*0.07 + G*0.72 + R* 0.21 #I found it online g.append(a) rgb2gray(img1) #convert the img1 into grayscale gr = np.array(g) #convert the list 'g' containing grayscale pixel values into numpy array cv2.imwrite("test1.png" , gr.reshape(row,col)) #save the image file as test1.jpg
`
4)
`
p = img.getPixel(col/factor,row/factor) newImage.setPixel(col,row,p)
`
5)
`
def is_prime(x): # only positive numbers are allowed and the smallest prime number is 2 if (x > 1): # since the smallest prime number is 2, we start the divisor at 3 divisor = 2 # because the submit number can always be divided by itself we can use the # range function to set the correct range for i in range(divisor,x): if (x % i) == 0: return False else: return False return True print(is_prime(3))
`