Question
Your submission will be an original program coded with the Python language. Include Comments and Modify the code below. There are three filters jpg images
Your submission will be an original program coded with the Python language. Include Comments and Modify the code below. There are three filters jpg images and 1 grey jpg image.
Your image filter program must:
Create three filters each of which manipulate the image at the pixel level. Of the three filters you create, two must create the filter based on user input. Use Python constructs to accomplish these tasks, including: creating functions, using selection (if/elif/else), using iteration (while and for ), and getting input. Include documentation explaining how the code segment functions for each filter. Your program should be aesthetically pleasing and easy to use.
# importing PIL.Image library and os library from PIL import Image #from PIL import Image import os
# Deletes old created images if they exist images = ["combinedFilters.jpg","filter1.jpg","filter2.jpg","filter3.jpg","grey.jpg"] for i in images: if os.path.exists(i): os.remove(i)
# Adds two blank lines before any output print(" ")
# Opens image - upload a Local File into repl.it img = Image.open('image.jpg')
# Rescale image size down, if original is too large width = img.width height = img.height mwidth = width // 1000 mheight = height // 1000 if mwidth > mheight: scale = mwidth else: scale = mheight if scale != 0: img = img.resize( (width // scale, height // scale) )
######################## # Example Filter # ######################## def grey(): # Creates an ImageCore Object from original image pixels = img.getdata() # Creates empty array to hold new pixel values new_pixels = [] # For every pixel from our original image, it saves # a copy of that pixel to our new_pixels array for p in pixels: new_pixels.append(p) # Starts at the first pixel in the image location = 0 # Continues until it has looped through all pixels while location < len(new_pixels): # Gets the current color of the pixel at location p = new_pixels[location] # Splits color into red, green and blue components r = p[0] g = p[1] b = p[2] # Perform pixel manipulation and stores results # to a new red, green and blue components newr = (r + g + b) // 3 newg = (r + g + b) // 3 newb = (r + g + b) // 3 # Assign new red, green and blue components to pixel # at that specific location new_pixels[location] = (newr, newg, newb) # Changes the location to the next pixel in array location = location + 1 # Creates a new image, the same size as the original # using RGB value format newImage = Image.new("RGB", img.size) # Assigns the new pixel values to newImage newImage.putdata(new_pixels) # Sends the newImage back to the main portion of program return newImage
##################### # Your Filter # #####################
def filter1(): print("Code for filter1") newImage = img return newImage
##################################### # Your Filters with User Input # #####################################
def filter2(): print("Code for filter2") newImage = img return newImage
def filter3(): print("Code for filter3") newImage = img return newImage
# Creates the four filter images and saves them to our files a = grey() a.save("grey.jpg") b = filter1() b.save("filter1.jpg") c = filter2() c.save("filter2.jpg") d = filter3() d.save("filter3.jpg")
# Image filter names for use below f1 = "filter1" f2 = "filter2" f3 = "filter3"
# Apply multiple filters through prompts with the user answer = input(" Which filter do you want me to apply? grey " + f1 + " " + f2 + " " + f3 + " none ") while answer != "grey" and answer != f1 and answer != f2 and answer != f3 and answer != "none": answer = input(" Incorrect filter, please enter: grey " + f1 + " " + f2 + " " + f3 + " none ")
while answer == "grey" or answer == f1 or answer == f2 or answer == f3: if answer == "grey": img = grey() elif answer == f1: img = filter1() elif answer == f2: img = filter2() elif answer == f3: img = filter3() else: break print("Filter \"" + answer + "\" applied...") answer = input(" Which filter do you want me to apply next? grey " + f1 + " " + f2 + " " + f3 + " none ") while answer != "grey" and answer != f1 and answer != f2 and answer != f3 and answer != "none": answer = input(" Incorrect filter, please enter: grey " + f1 + " " + f2 + " " + f3 + " none ") print("Image being created...Done")
# Create the combined filter image and saves it to our files img.save("combinedFilters.jpg")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started