Trying to create filters for images, getting integer argument expected errors, screenshots and code included

185
April 08, 2022, at 11:00 AM

so I am taking a Python course on Codio and the current topic is image manipulation, with the assignment being to create certain filters for images.

Our base code is ready, but when correcting errors, I am getting: TypeError: integer argument expected, got float. I understand the error, but I am just not sure where exactly it is in the program. Here is the full error in question (Link since I can't embed images yet)

And the lines that it refers to (with the lines around it for context:

          # Assigns the new pixel values to newImage
    117   newImage.putdata(new_pixels)
          # Sends the newImage back to the main portion of program
          return newImage
          a = gray()
          a.save("StudentWork/gray.jpg")
    212   b = filter1()
          b.save("StudentWork/filter1.jpg")
          c = filter2()
          c.save("StudentWork/filter2.jpg")
          d = filter3()
          d.save("StudentWork/filter3.jpg")

I am not exactly sure what the 3rd "Filter" in the error is referring to, but I think that it is an error with the file of the image in question, or getting through the directory and to the image. I am not exactly sure what is happening with the code and the int argument error.

Below is the full code for our program:

# 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 = ["StudentWork/combinedFilters.jpg","StudentWork/filter1.jpg","StudentWork/filter2.jpg","StudentWork/filter3.jpg","StudentWork/gray.jpg"]
for i in images:
    if os.path.exists(i):
        os.remove(i)
# Adds two blank lines before any output
print("\n\n")
# Opens image - upload a Local File into repl.it
img = Image.open('StudentWork/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 gray():
    print("Code for grayscale")
    # 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("Filter 1: Tint image red")
    # 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
        newg = r/3
        newb = r/6
        # 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 Filters with User Input   #
#####################################
def filter2():
    print("Filter 2: Saturate image")
    # 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
    x = int(input("By what factor do you want to saturate the image? "))
    # 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*x
        newg = g*x
        newb = b*x
        # 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
def filter3():
    print("Filter 3: Grid")
    #Creates an ImageCore Object from original image
    pixels = img.getdata()
    #Creates empty array to hold new pixel values
    new_pixels = []
    #create array to keep track of where gridlines are made
    black_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
    x = 100
    # Continues until it has looped through all pixels
    while location < len(new_pixels):
        if location % x == 0:
            # 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 = 0
            newg = 0
            newb = 0
            # Assign new red, green and blue components to pixel
            #at that specific location
            new_pixels[location] = (newr, newg, newb)
            #keep track of pixels changed
            black_pixels[location] = 1
            # 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
# Creates the four filter images and saves them to our files
a = gray()
a.save("StudentWork/gray.jpg")
b = filter1()
b.save("StudentWork/filter1.jpg")
c = filter2()
c.save("StudentWork/filter2.jpg")
d = filter3()
d.save("StudentWork/filter3.jpg")
# Image filter names for use below
f1 = "filter1"
f2 = "filter2"
f3 = "filter3"
# Apply multiple filters through prompts with the user
print("\nThe following prompt will ask you which filter to apply to the combined filter. It will keep asking until you answer 'none'.")
answer = input("\nWhich filter do you want me to apply?\n gray\n " +  f1 + "\n " + f2 + "\n " + f3 + "\n none\n\n")
while answer != "gray" and answer != f1 and answer != f2 and answer != f3 and answer != "none":
    answer = input("\nIncorrect filter, please enter:\n gray\n " +  f1 + "\n " + f2 + "\n " + f3 + "\n none\n\n")
while answer == "gray" or answer == f1 or answer == f2 or answer == f3:
    if answer == "gray":
        img = gray()
    elif answer == f1:
        img = filter1()
    elif answer == f2:
        img = filter2()
    elif answer == f3:
        img = filter3()
    else:
        break
    print("Filter \"" + answer + "\" applied...")
    answer = input("\nWhich filter do you want me to apply next?\n gray\n " +  f1 + "\n " + f2 + "\n " + f3 + "\n none\n\n")
    while answer != "gray" and answer != f1 and answer != f2 and answer != f3 and answer != "none":
        answer = input("\nIncorrect filter, please enter:\n gray\n " +  f1 + "\n " + f2 + "\n " + f3 + "\n none\n\n")
print("Combined filter being created...Done")
# Create the combined filter image and saves it to our files
img.save("StudentWork/combinedFilters.jpg")

Our professor is out indefinitely at the moment, and we don't have anyone to help us in class at the moment. I assume that if we are really struggling, he would be fine with us submitting it as is, but I want to try fixing all the errors.

I am still relatively new to asking questions here, so please let me know if I've done anything wrong. Any help is appreciated. Thank you!

EDIT I have solved the error by casting each item in the list as an int. When I tried the same thing before, it wasn't working, but now it is, so I must have forgotten a parenthesis or something like that. There are a few new errors with other filters, but I should be able to manage them. Thanks again!

Rent Charter Buses Company
READ ALSO
How to change between iframes in Selenium and Python

How to change between iframes in Selenium and Python

I was trying to understand how to change between iframes in selenium webdriver, but I could not figure it outIn the image ahead, is the iframe that I want to switch to:

116
Removing multiple headers in a single excel sheet

Removing multiple headers in a single excel sheet

Similar to Remove header row in Excel using pandas

86
How to bulk rename JSON files?

How to bulk rename JSON files?

I am trying to rename and replace a certain text with the hexadecimal for thousands of JSON files

70