Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

def blur(img: Image) -> Image: Blur Image, img, based on the given pixel_size Hints: - For each pixel, calculate average RGB values of its neighbouring

def blur(img: Image) -> Image: """Blur Image, img, based on the given pixel_size Hints: - For each pixel, calculate average RGB values of its neighbouring pixels (i.e. newRed = average of all the R values of each adjacent pixel, ...) - Set the RGB value of the center pixel to be the average RGB values of all the pixels around it Be careful at the edges of the image: not all pixels have 8 neighboring pixels! """ img_width, img_height = img.size pixels = img.load() avg_rbg = 0 for i in range(img_width): for j in range(img_height): # corner (top left) if j == 0 and i == 0: avg1 = int(sum(pixels[i+1, j]) / 3) avg2 = int(sum(pixels[i, j + 1]) / 3) avg3 = int(sum(pixels[i+1, j+1]) / 3) total_avg = int((avg1 + avg2 + avg3) / 3) avg_rbg += total_avg # corner (bottom left) elif (j == (img_height - 1)) and i == 0: avg1 = int(sum(pixels[i+1, j]) / 3) avg2 = int(sum(pixels[i, j-1]) / 3) avg3 = int(sum(pixels[i+1, j-1]) / 3) total_avg = int((avg1 + avg2 + avg3) / 3) avg_rbg += total_avg # corner (top right) elif j == 0 and (i == (img_width - 1)): avg1 = int(sum(pixels[i, j+1]) / 3) avg2 = int(sum(pixels[i-1, j+1]) / 3) avg3 = int(sum(pixels[i-1, j]) / 3) total_avg = int((avg1 + avg2 + avg3) / 3) avg_rbg += total_avg # corner (bottom right) elif (j == (img_height - 1)) and (i == (img_width - 1)): avg1 = int(sum(pixels[i-1, j]) / 3) avg2 = int(sum(pixels[i, j-1]) / 3) avg3 = int(sum(pixels[i-1, j-1]) / 3) total_avg = int((avg1 + avg2 + avg3) / 3) avg_rbg += total_avg # row 1 elif i == 0 and (not ((j == 0 and i == 0) and ((j == (img_height - 1)) and i == 0))): avg1 = int(sum(pixels[i, j+1]) / 3) avg2 = int(sum(pixels[i+1, j-1]) / 3) avg3 = int(sum(pixels[i+1, j]) / 3) avg4 = int(sum(pixels[i+1, j+1]) / 3) avg5 = int(sum(pixels[i, j-1]) / 3) total_avg = int((avg1 + avg2 + avg3 + avg4 + avg5) / 5) avg_rbg += total_avg # row 2 elif i == img_width and (not (j == 0 and (i == (img_width - 1))) and ((j == (img_height - 1)) and (i == (img_width - 1)))): avg1 = int(sum(pixels[i, j-1]) / 3) avg2 = int(sum(pixels[i-1, j+1]) / 3) avg3 = int(sum(pixels[i-1, j]) / 3) avg4 = int(sum(pixels[i-1, j-1]) / 3) avg5 = int(sum(pixels[i, j+1]) / 3) total_avg = int((avg1 + avg2 + avg3 + avg4 + avg5) / 5) avg_rbg += total_avg # column 1 elif j == 0 and (not (j == 0 and (i == (img_width - 1)))): avg1 = int(sum(pixels[i-1, j]) / 3) avg2 = int(sum(pixels[i-1, j+1]) / 3) avg3 = int(sum(pixels[i, j+1]) / 3) avg4 = int(sum(pixels[i+1, j]) / 3) avg5 = int(sum(pixels[i+1, j+1]) / 3) total_avg = int((avg1 + avg2 + avg3 + avg4 + avg5) / 5) avg_rbg += total_avg # column 2 elif (j == (img_height - 1)) and (not (j == (img_height - 1)) and ((i == (img_width - 1)) and ((j == (img_height - 1)) and i == 0))): avg1 = int(sum(pixels[i-1, j]) / 3) avg2 = int(sum(pixels[i-1, j-1]) / 3) avg3 = int(sum(pixels[i, j-1]) / 3) avg4 = int(sum(pixels[i+1, j]) / 3) avg5 = int(sum(pixels[i+1, j-1]) / 3) total_avg = int((avg1 + avg2 + avg3 + avg4 + avg5) / 5) avg_rbg += total_avg else: avg1 = int(sum(pixels[i+1, j]) / 3) avg2 = int(sum(pixels[i+1, j+1]) / 3) avg3 = int(sum(pixels[i+1, j-1]) / 3) avg4 = int(sum(pixels[i, j+1]) / 3) avg5 = int(sum(pixels[i, j-1]) / 3) avg6 = int(sum(pixels[i-1, j]) / 3) avg7 = int(sum(pixels[i-1, j+1]) / 3) avg8 = int(sum(pixels[i-1, j-1]) / 3) total_avg = int((avg1 + avg2 + avg3 + avg4 + avg5 + avg6 + avg7 + avg8) / 8) avg_rbg += total_avg r, g, b = pixels[i, j] pixels[i, j] = (avg_rbg, avg_rbg, avg_rbg) return img 

What you can use from the Image module:

- Image.new(mode, size, color): creates a new image (use 'RGB' for mode)

- Image.open(filename): opens an existing image file

- Image.size: returns the image size in pixels as a 2-tuple, (width, height)

- Image.load(): returns a PixelAccess object of all of the image's pixels

- Image.show(): display the image represented by the Image object

- Image.save(filename): saves an image

- Image.close(): closes an open Image file

You CANNOT use any other Image method, and you cannot import any other Python module, except for what we already imported for you.

This is my code for the following function, but it does not seem to work. What can I change in the code to make it work according to the docstring.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

What were the organizational issues at Wells Fargo?

Answered: 1 week ago