Question

Very stuck on creating a python program for the below question: Write a program that has...

Very stuck on creating a python program for the below question:

Write a program that has three functions: sepia(), remove_all_red(), and gray_scale() to process the image. Plot all four images. No global variables are allowed. Each function needs parameter(s) in order to manipulate and draw the image.

Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows:

newR = (R × 0.393 + G × 0.769 + B × 0.189)

newG = (R × 0.349 + G × 0.686 + B × 0.168)

newB = (R × 0.272 + G × 0.534 + B × 0.131)

Red removal from an image:

Simply set the R component to 0.

Gray scale conversion:

                        newR = (R × 0.289 + G × 0.587 + B × 0.114)

newG = (R × 0.289 + G × 0.587 + B × 0.114)

newB = (R × 0.289 + G × 0.587 + B × 0.114)

where R, G, and B are the original image pixel red, green and blue values.

Hint: Remember that RGB values must be integers between 0 and 255.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#for uploading files into Google colab online
from google.colab import files
f = files.upload()

import numpy
#PIL library for imagee processing
from PIL import Image
#IPython library for showing images in google colab
from IPython import display


#function for grayscale conversion of image ;input:image ; output:grayscale image
def gray_scale():
#Open the image using PIL library Image.open
img = Image.open('p_1_4_a.jpg')
#convert the image into a numpy array
rgb = numpy.array(img)

#Iterate through all rows
for i in range(len(rgb)):
#Iterate through all columns
for j in range(len(rgb[1])):
temp = []
#apply given formula
newR = (0.289*rgb[i][j][0])+(0.587*rgb[i][j][1])+(0.114*rgb[i][j][2])
newG = (0.289*rgb[i][j][0])+(0.587*rgb[i][j][1])+(0.114*rgb[i][j][2])
newB = (0.289*rgb[i][j][0])+(0.587*rgb[i][j][1])+(0.114*rgb[i][j][2])
#assign the value to the duplicate array
temp.append(newR)
temp.append(newG)
temp.append(newB)   
rgb[i][j] = temp
#set the values into uint8 form and convert it into numpy array
rgb = numpy.asarray(rgb.astype('uint8'))
#make an image from array
gray = Image.fromarray(rgb)
print("Input Image")
#Displaying the input rgb image
display.display(img)
print("Grayscale Image")
#Displaying the output Grayscale image
display.display(gray)
return

  
  
#Function call for Grayscale Image
gray_scale()

#function for sepia conversion of image ;input:image ; output:sepia image
def sepia():
#Open the image using PIL library Image.open
img = Image.open('p_1_4_a.jpg')
#convert the image into a numpy array
rgb = numpy.array(img)

#Iterate through all rows
for i in range(len(rgb)):
#Iterate through all columns
for j in range(len(rgb[1])):
temp = []
#apply given formula
newR = (0.393*rgb[i][j][0])+(0.769*rgb[i][j][1])+(0.189*rgb[i][j][2])
newG = (0.349*rgb[i][j][0])+(0.686*rgb[i][j][1])+(0.168*rgb[i][j][2])
newB = (0.272*rgb[i][j][0])+(0.534*rgb[i][j][1])+(0.131*rgb[i][j][2])
#assign the value to the duplicate array
temp.append(newR)
temp.append(newG)
temp.append(newB)   
rgb[i][j] = temp
#set the values into uint8 form and convert it into numpy array
rgb = numpy.asarray(rgb.astype('uint8'))
#make an image from array
gray = Image.fromarray(rgb)
print("Input Image")
#Displaying the input rgb image
display.display(img)
print("sepia Image")
#Displaying the output sepia image
display.display(gray)
return   

sepia()

#function for all red removal of image ;input:image ; output:red removed image
def remove_all_red():
#Open the image using PIL library Image.open
img = Image.open('p_1_4_a.jpg')
#convert the image into a numpy array
rgb = numpy.array(img)

#Iterate through all rows
for i in range(len(rgb)):
#Iterate through all columns
for j in range(len(rgb[1])):
temp = []
#apply given formula
newR = 0.0
newG = (0.349*rgb[i][j][0])+(0.686*rgb[i][j][1])+(0.168*rgb[i][j][2])
newB = (0.272*rgb[i][j][0])+(0.534*rgb[i][j][1])+(0.131*rgb[i][j][2])
#assign the value to the duplicate array
temp.append(newR)
temp.append(newG)
temp.append(newB)   
rgb[i][j] = temp
#set the values into uint8 form and convert it into numpy array
rgb = numpy.asarray(rgb.astype('uint8'))
#make an image from array
gray = Image.fromarray(rgb)
print("Input Image")
#Displaying the input rgb image
display.display(img)
print("sepia Image")
#Displaying the output image
display.display(gray)
return   

remove_all_red()

#outputs:

Add a comment
Know the answer?
Add Answer to:
Very stuck on creating a python program for the below question: Write a program that has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • from PIL import Image import random # NOTE: Feel free to add in any constant values...

    from PIL import Image import random # NOTE: Feel free to add in any constant values you find useful to use BLACK = (0, 0, 0) WHITE = (255, 255, 255) # NOTE: Feel free to add in any helper functions to organize your code but # do NOT rename any existing functions (or else, autograder # won't be able to find them!!) # NOTE: The following function is already completed for you as an example # You can use...

  • I am using Ocelot to write a program in JavaScript Our question is below: Write a...

    I am using Ocelot to write a program in JavaScript Our question is below: Write a function called removeBlueAndGreen that takes an image as an argument and returns a red version of the input image. To do so, create a copy of the input and iterate over each pixel. If the color of a pixel is (r, g, b) in the input image, its color in the output must be (r, 0.0, 0.0). Now is I write let robot =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT