Question

4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels containing pixels for an image, anNotes/hints: Here again, your function will need to create and return a new 2-D list of pixels, and you can use the blank_imadef create_uniform_image(height, width, pixel): creates and returns a 2-D list of pixels with height rows and width columPlease design the function in version 3 of python

4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list that represents the portion of the original image that is specified by the other four parameters. The extracted portion of the image should consist of the pixels that fall in the intersection of the rows of pixels that begin with row rmin and go up to but not including row rmax the columns of pixels that begin with column cmin and go up to but not including column cmax. For example, if you do the following: pixels - load_pixels('spam.png' extracted extract(pixels, 90, 150, 75, 275) save-pixels(extracted, 'extract spam.png) extract_spam.png saved. you should obtain the following image: SPAM
Notes/hints: Here again, your function will need to create and return a new 2-D list of pixels, and you can use the blank_image) function for that purpose. Compute the dimensions of the new image from the inputs to the function. - Don't forget that the rmax and cmax parameters are exclusive, which means that the extracted image should not include pixels from row rmax or column cmax of the original image. This is similar to what happens when you slice a string or list, and it should make it easier to compute the dimensions of the image. One way to solve this problem is to use nested loops to iterate over the rows and columns of the newly created 2-D list-the one that represents the extracted image. You can then perform the necessary computations on your loop variables r and c to determine the cooordinates of the pixel from the original image that you should use for the pixel at position (r, c) in the new image. Use concrete cases to determine the appropriate computations.
def create_uniform_image(height, width, pixel): """ creates and returns a 2-D list of pixels with height rows and width columns in which all of the pixels have the RGB values given by pixel inputs: height and width are non-negative integers pixel is a 1-D list of RBG values of the form [R,G,B], where each element is an integer between 0 and 25!5 pixels - for r in range(height): row - [pixel] * width pixels +- [row] return pixels def blank_image(height, width): "" creates and returns a 2-D list of pixels with height rows and width columns in which all of the pixels are green. inputs: height and width are non-negative integers all_green - create_uniform_image(height, width, [0, 255, 0]) return all_green def brightness(pixel): """takes a pixel (an [R, G, B] list) and returns a value between 0 and 255 that represents the brightness of that pixel red -pixel[0] green pixel[1] blue -pixel[2] return (21*red 72*green 7*blue) // 100
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def extract(pixels,rmin,rmax,cmin,cmax):
  
'''
  
This function return the extracted image.
  
It simply loops over required rows and columns
and stores the result in another list which is
returned.
  
'''
  
ans=[]
for i in range(rmin,rmax):
row=[]
for j in range(cmin,cmax):
row.append(pixels[i][j])
ans.append(row)
  
return ans

Add a comment
Know the answer?
Add Answer to:
4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels contai...
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
  • Please use python. You can import only: from typing import Dict, List from PIL import Image...

    Please use python. You can import only: from typing import Dict, List from PIL import Image import random Questions are: --------------------------------------------------------------------------------------------------------------------------------- 1. def rotate_picture_90_left(img: Image) -> Image: """Return a NEW picture that is the given Image img rotated 90 degrees to the left. Hints: - create a new blank image that has reverse width and height - reverse the coordinates of each pixel in the original picture, img, and put it into the new picture """ -----------------------------------------------------------------------------------------------------------------------------------    2. def...

  • 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...

  • Python Project

    AssignmentBitmap files map three 8-bit (1-byte) color channels per pixel. A pixel is a light-emitting "dot" on your screen. Whenever you buy a new monitor, you will see the pixel configuration by its width and height, such as 1920 x 1080 (1080p) or 3840x2160 (4K). This tells us that we have 1080 rows (height), and each row has 1920 (width) pixels.The bitmap file format is fairly straight forward where we tell the file what our width and height are. When...

  • Problem1: BMPmain.c, BMPfns.c, BMPfns.h, Makefile The file 'BMPmain.c' contains a main() function which is already written...

    Problem1: BMPmain.c, BMPfns.c, BMPfns.h, Makefile The file 'BMPmain.c' contains a main() function which is already written for you and attached with this homework. When appropriate functions are provided, main() will create a .bmp image file. Your job is to write 'BMPfns.c' which contains the functions which main() uses to create .bmp image files. You must also write the header file 'BMPfns.h' to #include in BMPmain.c and which contains prototypes of the functions defined in BMPfns.c . Problem2: BMPcheckerboard.c, BMPfns.c, BMPfns.h,...

  • Using Python Programming Language: 4. Implement the following function in the PyDev module functions.py and test...

    Using Python Programming Language: 4. Implement the following function in the PyDev module functions.py and test it from a PyDev module named t04.py: def print_matrix_char(matrix): Prints the contents of a 2D list of strings in a formatted table. Prints row and column headings. Use: print_matrix_char (matrix) Parameters: matrix - a 2D list of strings (2D list) Returns: None. Sample testing: Number of rows: 3 Number of columns: 4 0 1 2 3 i 0 с u r 1 с у...

  • Encoding and Decoding PPM image 3 & 4 are parts of question 2 Code for a09q1:...

    Encoding and Decoding PPM image 3 & 4 are parts of question 2 Code for a09q1: We were unable to transcribe this image3 Encoding an Image in a09q2.py In the next four sections, you will no longer write class methods but rather functions. In each of the next four parts, you should be importing the PPM class from a09q1.py to use with the command from a09q1 import PPM Note that this is similar to how the check module was imported...

  • Implement the following function in the PyDev module functions.py and test it from a PyDev module...

    Implement the following function in the PyDev module functions.py and test it from a PyDev module named t04.py: def print_matrix_char (matrix): وق في ال Prints the contents of a 2D list of strings in a formatted table. Prints row and column headings. Use: print_matrix_char (matrix) Parameters: matrix Returns None. a 2D list of strings (2D list) w Sample testing: Number of rows: 3 Number of columns: 4 0 1 2 3 i 0 C u r 1 Z 2 Y...

  • FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter....

    FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter. It returns the index of the minimum row in the list. The minimum row is the row with the smallest sum of elements. If the list has multiple rows that achieve the minimum value, the last such row should be returned. If the list is empty the function should return -1. The following shows several sample runs of the function: Python 3.4.1 Shell File...

  • Your program should be capable of creating a ppm image of the flag of Benin • The new flag gener...

    Your program should be capable of creating a ppm image of the flag of Benin • The new flag generated should use the proper width-to-height ratio as defined on Wikipedia for the flag. o For the colors, use pure red, and yellow, and a value of 170 for green. ▪ Pure color values are talked about in Lab 6. o The left green field should be 6/15ths of the width of the flag. Variables country_code should also include a new...

  • The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have...

    The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have been placed above a conveyer belt to enables parts on the belt to be photographed and analyzed. You are to augment the system that has been put in place by writing C code to detect the number of parts on the belt, and the positions of each object. The process by which you will do this is called Connected Component Labeling (CCL). These positions...

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
ADVERTISEMENT