Question

In: Computer Science

Complete this in simple python code. Sepia Tone images are those brownish colored images that may...

Complete this in simple python code.
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)
.............................................................................
Write a function to convert an image to sepia tone. Remember that rgb values must be integers between 0 and 255...

Solutions

Expert Solution

Image conversion code according to above formula is
CODE 1 :
from PIL import Image     #using Image from pillow library

def sepia(img_path:str)->Image:
    img = Image.open(img_path)
    width, height = img.size

    pixels = img.load()       #creating the pixel map

    for y in range(height):
     for x in range(width):
         r, g, b = img.getpixel((x, y))

         new_r = int(0.393 * r + 0.769 * g + 0.189 * b)
         new_g = int(0.349 * r + 0.686 * g + 0.168 * b)
         new_b = int(0.272 * r + 0.534 * g + 0.131 * b)

         if new_r > 255:       #r,g,b should not exceed 255
             new_r = 255

         if new_g > 255:
             new_g = 255

         if new_b > 255:
             new_b = 255

         pixels[x, y] = (new_r,new_g,new_b)

    return img

You can also use this simple formula instead for conversion (it is simple and effective).

CODE 2 :

from PIL import Image     #using Image from pillow library

def sepia(img_path:str)->Image:
    img = Image.open(img_path)
    width, height = img.size

    pixels = img.load()        #creating the pixel map

    for y in range(height):
     for x in range(width):
         r, g, b = img.getpixel((x, y))
         new_val = (0.3 * r + 0.59 * g + 0.11 * b)
         
         new_r = int(new_val * 2)
         if new_r > 255:       #r,g,b should not exceed 255
             new_r = 255
         new_g = int(new_val * 1.5)
         if new_g > 255:
             new_g = 255
         new_b = int(new_val)
         if new_b > 255:
             new_b = 255

         pixels[x, y] = (new_r,new_g,new_b)

    return img

Related Solutions

For Each question the starter code is listed below Make shure complete in simple Python 1)Write...
For Each question the starter code is listed below Make shure complete in simple Python 1)Write a function that removes all occurrences of a given letter from a string. def remove_letter(theLetter, theStri 2)Write a function that reverses its string argument. def reverse(astring): 3)Write a function that implements a substitution cipher. In a substitution cipher one letter is substituted for another to garble the message. For example A -> Q, B -> T, C -> G etc. Your function should take...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
Write a simple matching coefficient and jaccard similarity code in python. For a example x =...
Write a simple matching coefficient and jaccard similarity code in python. For a example x = 10101 and y = 00101 what is the code to check those similarities in python?
In simple python code 1) Write a function to convert the image to grayscale. 2Write a...
In simple python code 1) Write a function to convert the image to grayscale. 2Write a function to convert an image to black and white 3)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 ×...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random...
Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random matrix size 5x5.
Complete the code so that it can convert the date to day of week using python,...
Complete the code so that it can convert the date to day of week using python, the code should pass the doctest def convert_datetime_to_dayofweek(datetime_string): """ This function takes date in the format MON DAY YEAR HH:MM(PM/AM) and returns the day of the week Assume input string is UTC    >>> convert_datetime_to_dayofweek('Jun 1 2005 1:33PM') 'Wednesday' >>> convert_datetime_to_dayofweek('Oct 25 2012 2:17AM') 'Thursday' """ # code goes here
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT