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
  • int loadVerticalSeam(Pixel** image, int start_col, int width, int height, int* seam); This function will traverse through...

    int loadVerticalSeam(Pixel** image, int start_col, int width, int height, int* seam); This function will traverse through an image starting at the first row at the given start_col. See “Loading a vertical seam” below for how the traversal works. See “Seam Representation” below for how seams are represented. The function returns the total energy of the seam The first parameter is a 2d array of Pixels (structs) that hold a color value The second parameter is the column to start the...

  • Please write a code in python! Define and test a function named posterize. This function expects...

    Please write a code in python! Define and test 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, but it uses the given RGB values instead of black. images.py import tkinter import os, os.path tk = tkinter _root = None class ImageView(tk.Canvas): def __init__(self, image, title = "New Image", autoflush=False): master = tk.Toplevel(_root) master.protocol("WM_DELETE_WINDOW", self.close) tk.Canvas.__init__(self, master, width = image.getWidth(), height = image.getHeight())...

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

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