Question

In: Computer Science

Define a function named posterize. This function expects an image and a tuple of RGB values...

Define a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function developed in Chapter 7 and shown below, but it uses passed in RGB values instead of black.

def blackAndWhite(image):
    """Converts the argument image to black and white."""
    blackPixel = (0, 0, 0)
    whitePixel = (255, 255, 255)
    for y in range(image.getHeight()):
        for x in range(image.getWidth()):
            (r, g, b) = image.getPixel(x, y)
            average = (r + g + b) // 3
            if average < 128:
                image.setPixel(x, y, blackPixel)
            else:
                image.setPixel(x, y, whitePixel)

An example of the program is shown below:

"""

File: posterize.py

Project 7.5

Defines and tests a function for posterizing images.

"""

from images import Image


""" Write your code here """

def main():

    filename = input("Enter the image file name: ")

    red = int(input("Enter an integer [0..255] for red: "))

    green = int(input("Enter an integer [0..255] for green: "))

    blue = int(input("Enter an integer [0..255] for blue: "))                    

    image = Image(filename)

    posterize(image, (red, green, blue))

    image.draw()

if __name__ == "__main__":

   main()


Solutions

Expert Solution

I hope you are asking for a function , that can explicitly set the RGB values for the input image, (if I am wrong in this statement, post a comment below, I will help based on that)

The required function is :(updated code),

I really want say, l sont the library you are usinh, Image. Intrsad I used teh opensourse project

from PIL import Image 
def posterize(image, rgb_values):
  '''Converts the argument image to the pixels passed'''
  # for y in range(image.height):
  #   for x in range(image.width):
  #     # image.setPixel(x, y, rgb_values) # directly put the required rgb tuple in the current pixel position
  #     image.putpixel((x,y),rgb_values)


  whitePixel = (255, 255, 255)
  for y in range(image.getHeight()):
    for x in range(image.getWidth()):
      (r, g, b) = image.getPixel(x, y)
      average = (r + g + b) // 3
      if average < 128:
        image.setPixel(x, y, rgb_values)
      else:
        image.setPixel(x, y, whitePixel)

  return image

def main():
  # filename = input("Enter the image file name: ")
  # red = int(input("Enter an integer [0..255] for red: "))
  # green = int(input("Enter an integer [0..255] for green: "))
  # blue = int(input("Enter an integer [0..255] for blue: "))    
  filename = 'panda.jpg'
  red = 100
  green = 100
  blue = 100                
  image = Image.open(filename)
  
  posterize(image, (red, green, blue))
  # image.draw()

if __name__ == "__main__":
  main()

Screenshot of the code : ( to understand the indentation)


Related Solutions

create a function that sorted tuple by the second values of each tuple. If the values...
create a function that sorted tuple by the second values of each tuple. If the values are the same, sorted by increasing order. Finally returns a list of the first element of each tuple that sorted. def sort_tup(tup): #Code here input : tup1 = [(1, 15), (2, 8), (3, 22), (4, 30), (5, 15)] output: [4,3,1,5,2]
You will overload a function named printArray. The function will assign values to each element and...
You will overload a function named printArray. The function will assign values to each element and print out a single dimension array of integers and an array of characters. The main body of the program will call each function. In C++
in C++ You will overload a function named printArray. The function will assign values to each...
in C++ You will overload a function named printArray. The function will assign values to each element and print out a single dimension array of integers and an array of characters. The main body of the program will call each function. (6 points) Using the player switch portion of the tic tac toe game, create a function that does the switch. This is a practice in applying local variables, parameters, and global variables. (4 points)
Write code to define a function named mymath. The function has three arguments in the following...
Write code to define a function named mymath. The function has three arguments in the following order: Boolean, Integer, and Integer. It returns an Integer. The function will return a value as follows: 1. If the Boolean variable is True, the function returns the sum of the two integers. 2. If the Boolean is False, then the function returns the value of the first integer - the value of the second Integer.
(Python) Define a function makeTwoWay that expects a singly linked structure as its argument. The function...
(Python) Define a function makeTwoWay that expects a singly linked structure as its argument. The function builds and returns a doubly linked structure that contains the items in the singly linked structure. (Note: The function should not alter the argument's structure.) node.py source code: """ File: node.py Node classes for one-way linked structures and two-way linked structures. """ class Node(object):     def __init__(self, data, next = None):         """Instantiates a Node with default next of None"""         self.data = data...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the second highest number from the array. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input array will always have length at least two (so there is always a second highest value). The original array must NOT be modified by this function. Example secondHighest([5,3,8,6,2,7,4]) must return 7, and secondHighest([5,3,8,6,2,7,8]) must return 8. I have...
Define an array named PeopleTypes that can store a maximum of 50 integer values that will be entered at the keyboard.
Define an array named PeopleTypes that can store a maximum of 50 integer values that will be entered at the keyboard. Enter a series of 1s, 2s,3s and 4s, into the array, where a represent an infant, a 2 represent a child, a 3 represents a teenager, and a 4 represents an adult who was present at a local school functionp.412 PE 4Write a C function that finds and displays the maximum value in a two dimensional array of integers....
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter is true, the function returns current hours; if the Boolean parameter is false, the function returns current month. o This function will obtain the current system time and extract the hours or the month value from the system time depending on the Boolean being received. o This function will return the extracted value as an integer value. o Note that the system time displays...
Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an...
Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an object of the type "string" and "ch" is a character value with the default argument of 'A'. The function looks for the character "ch" in the string  "s" and displays all the indices of its occurrences on the screen. For example, if the caller sends the values of "ABCDBGAB" and 'B' to the function for the parameters "s" and "ch", respectively, the function displays the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT