Question: Please write a similar code to this example, prompt is on the bottom. from typing import List def get _ adjacent _ locations ( row:

Please write a similar code to this example, prompt is on the bottom.
from typing import List
def get_adjacent_locations(row: int, col: int, m: int, n: int)-> List[List[int]]:
"""
Given a particular location, it returns all the adjacent locations WITHIN the boundaries of the grid
"""
return [[r, c] for r, c in [[row -1, col -1],[row -1, col],[row -1, col +1],[row, col -1],[row, col],[row, col +1],[row +1, col -1],[row +1, col],[row +1, col +1]] if 0<= r < m and 0<= c < n]
def get_average(locations: List[List[int]], image)-> List[int]:
"""
Take in a list of the locations within which we need to average out the r, g, b values.
"""
r = sum(image[i][j][0] for i, j in locations)/ len(locations)
g = sum(image[i][j][1] for i, j in locations)/ len(locations)
b = sum(image[i][j][2] for i, j in locations)/ len(locations)
return [r, g, b]
def compress(orig: List[List[List[int]]])-> List[List[List[int]]]:
m = len(orig)
n = len(orig[0])
new_image =[]
for row in range(len(orig)):
new_row =[]
for col in range(len(orig[0])):
red, green, blue = orig[row][col]
if row %5==0 and col %5==0:
# Get the locations adjacent to this
adjacent_locations = get_adjacent_locations(row, col, m, n)
# get the averaged out new color based on this locations
new_color = get_average(adjacent_locations, orig)
# add this new color to row of new image
new_row.append(new_color)
# row of new image complete so add it to new image
if row %5==0:
new_image.append(new_row)
return new_image
test_image =[[None for c in range(25)] for r in range(25)]
for r in range(25):
for c in range(25):
if r %2== c %2:
test_image[r][c]=[1,1,1]
else:
test_image[r][c]=[0,0,0]
print(len(test_image), len(test_image[0]))
new_image = compress(test_image)
print(len(new_image), len(new_image[0]))
print(new_image)
Prompt:
Please consider the following compression algorithm. As an input, you receive a image. This image is formatted as
follows. The image is a 3-dimensional array (if you are using python it could be thought of as a 2d array where the
individual elements are tuples (r, g, b). Write a compression algorithm which takes in an image and only
includes the pixels with location (r, c) such that r is divisible by 5 and s is divisible by 5. For those locations to be
included, the new value is the average of the surrounding values.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!