Question

def blur(img: Image) -> Image: """Blur Image, img, based on the given pixel_size Hints: - For...

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!
    """

please use python

only the following methods are allowed to use

- 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

provide notes beside each line of code would be appreciate thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE TO COPY:

# Imports PIL module
from PIL import Image

#average function to take a list and find average values of RGB and return that RGB tuple
def avg(x):
a=b=c=0
for i in x:
a=a+i[0]
b=b+i[1]
c=c+i[2]
s=len(x)
return (int(a/s),int(b/s),int(c/s))


def blur(im):
#load() function gives us the pixel object which is used to access any pixel
am = im.load()
  
#getting our image size and width so that to create new image of same size
width=im.size[0]
height=im.size[1]
  
#creating new image of same size
  
ima=Image.new(mode="RGB",size=(width,height))
#getting pixel object of new image
ama=ima.load()
  
#iterating throughout the image ans storing its average of around 8 pixels
for i in range(width-1):
for j in range(height-1):
ama[i,j]=avg([am[i-1,j-1],am[i-1,j],am[i-1,j+1],am[i,j-1],am[i,j+1],am[i+1,j-1],am[i+1,j],am[i+1,j+1]])

#showing image
ima.show()
  
#saving image
ima.save("new.png")
  
# open method used to open different extension image file
im = Image.open("code.png")
blur(im)

Add a comment
Know the answer?
Add Answer to:
def blur(img: Image) -> Image: """Blur Image, img, based on the given pixel_size Hints: - For...
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...

  • Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triangle, as a 24- bit RGB PNG image file. Specifications The command-line arg...

    Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triangle, as a 24- bit RGB PNG image file. Specifications The command-line arguments shall consist of the following 1. The width (in pixels) of the image, as a positive decimal integer 2. The height (in pixels) of the image, as a positive decimal integer 3. The minimum area (in pixels) that a triangle must have in order to be drawn,...

  • Please assist. I keep getting the error message in the middle screen when I run the...

    Please assist. I keep getting the error message in the middle screen when I run the program on the left. The image on the right is the section of images.py where it's indicating the issue with the .split is. How do I fix this? images.cy - /Users/carrietarpy Downloads/lab9-3/images.py 12.7.18) Python 2.7.18 Shell Python 2.7.15 (v2.7.18:542 102112, Apr 19 2820, 29:48:48) [GCC 4.7.1 Compatible Apple II w 6.3 Clong-FA3.0.57)] on der in Type "help", "copyright', 'credits' or "license()' for more inforyotion...

  • 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels contai...

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

  • In c++ The iron-puzzle.ppm image is a puzzle; it contains an image of something famous, howeve...

    In c++ The iron-puzzle.ppm image is a puzzle; it contains an image of something famous, however the image has been distorted. The famous object is in the red values, however the red values have all been divided by 10, so they are too small by a factor of 10. The blue and green values are all just meaningless random values ("noise") added to obscure the real image. If you were to create a grayscale image out of just the red...

  • Hi. I require your wonderful help with figuring out this challenging code. import java.awt.Color; import java.awt.image.BufferedImage;...

    Hi. I require your wonderful help with figuring out this challenging code. import java.awt.Color; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; public class ImageLab { /* * This is the grayscale example. Use it for inspiration / help, but you dont * need to change it. * * Creates and returns a new BufferedImage of the same size as the input * image. The new image is the grayscale version of the input (red, green, and * blue components averaged together)....

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

  • The picture is given in a PPM file and your program should put the converted one...

    The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the converted file.In addition, you can use a temporary file called tmp.ppm. •The number of rows and columns are not fixed numbers. •The converted file should also follow the PPM format with the above simplification, and can be converted subsequently. •Read the pixel matrix into a buffer. •For each row i(...

  • use MATLAB to upload the following: an image that you want to process (can be taken...

    use MATLAB to upload the following: an image that you want to process (can be taken yourself or downloaded from the internet) a script that processes the image in TWO ways. manipulates the colors averages pixels together Please make sure the script displays the images (like how I did with the 40 and 80 pixel averaging) so I can easily compare them to the original. Make sure to COMMENT your code as well. Homework 13 Please upload the following: an...

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