In: Computer Science
how to write a function that clears every pixel and sets it to the Red color. similarly, we have to open the image including height and width and use while loop that accesses each pixel in Python
Code:
#Load and show an image with Pillow
from PIL import Image
'''
THE FUNCTION
It takes an image convert to red and returns the image
'''
def toRed(img):
width, height = img.size
i,j = 0,0
while(i<width):
j=0
while(j<height):
img.putpixel((i,j),(255,0,0))
j+=1
i+=1
return img
#TESTING THE FUNCTION
img = Image.open('Lenna.png')
redImg = toRed(img)
redImg.show()
Here is the result before and after:
Hope this helps.