# Name: # Date: # Class: # Pledge: # Description: #This function reads in the first 3 lines of the infile #and writes those 3 lines out to the outfile #Parameters: infile (fileObject that you're reading from), # outfile(fileObject that you're writing to) #Returns: None def process_header(infile, outfile): pass # remove this line (including the 'pass') when starting to write this function. #This function reads in the body of a PPM file, one line at a time #Each line is split apart and put into 3 1-D lists (red, green, and blue) #red, green, and blue will be parallel lists, such that red[0], green[0], and blue[0] will #all refer to the rgb values for the 1st pixel in the line #Hint: Recall that the split function for strings allows you to either specify exactly #how many variables to create, or if you don't know, you can return the result into #a single variable, which will be a list of strings #Example: s = "5,4,2,5,6,7" # values = s.split(',') #values is now equal to ['5','4','2','5','6','7'] # # #Process each line individiually. #Call the appropriate modification functions on the correct color lists #Write out the modified lists to the outfile # #Parameters: infile (fileObject that you're reading from), # outfile(fileObject that you're writing to), # modification (string explaining which modifications the use wants) #Returns: None def process_body(infile, outfile, modification): pass # remove this line (including the 'pass') when starting to write this function. # Algorithm: (You can remove all these comments when you're done.) # Loop over the infile object 1 line at a time # strip off whitespace from the end of line # split the line into a list of strings # create 3 empty lists, one for red, one for blue, and one for green # # loop over the list of strings # for each rgb triple in the list, put the red value into the red list, # green into green list, and blue into blue list # # SAVE THIS PART UNTIL YOU GET THE OUTPUT FILE WORKING # once the entire line has been put into lists, call the appropriate filters # on the correct lists depending on the modification # For example, if modification is the negate red option, your code # should call the negate function and pass in the red list # # # create a new string variable that is empty # Write a loop that iterates over each index in red list # Use string concatentation to put together the red, green, and blue values # in the appropriate order, with spaces in between, into the new string you created # add an endline character to the end of the new string # write this new string to the outfile # Hint: print out this string, if you haven't done any modifications # it should be the exact same string as the original line from the infile #This file takes in a list and negates each value in the list #To negate a value, subtract 255 and take the absolute value of the result #Parameters: lst, a 1-D list of integers #Returns: a list with the modified values (negatives of the original values) def negate(lst): pass # remove this line (including the 'pass') when starting to write this function. #This file takes in a list and flattens each value in the list #To flatten a value, set it equal to zero #Parameters: lst, a 1-D list of integers #Returns: a list of the same length as (lst) with all zeros def flatten(lst): pass # remove this line (including the 'pass') when starting to write this function. #This file takes in three 1-D lists, and returns 3 lists that contain the average #of the original values at each index #To calculate the new value for each list, we need to take the average of the values #and replace the value with the average in all 3 lists (these values must be integers) #Example: red[0] = 30, green[0] = 100, blue[0] = 74, average = (30 + 100 + 74) / 3 = 68 #so we set red[0] = 68, green[0] = 68 and blue[0] = 68. #Hint: Use parallel lists #Parameters: red, green, blue (3 1-D lists with the same number of elements in them) #Returns: the updated lists, red, green, blue def grey_scale(red, green, blue): #Add your code here to correctly modify all 3 lists return red, green, blue #This file takes in a list and reverses the order of the list #Parameters: lst, a 1-D list of integers #Returns: the original list in reverse order def flip_horizontal(lst): pass # remove this line (including the 'pass') when starting to write this function. #This function takes in a filename and modification and outputs the correct #output file name #The output file name should retain the name of the input file, but also #include your last name and first name and the modification made to the file #Example: makeOutputFileName("BCLC.ppm", '3') returns "BCLC_welsh_catie_negate_green.ppm" #Parameters: filename (a string that includes a 3 character extension), # mod (the selected menu option) def makeOutputFileName(filename, mod): outputFileName = '' return outputFileName # the main function is mostly written for you. # Add an input validation loop where specified to ensure # valid input from the user def main(): #get input from the user inputFileName = input("What file would you like to modify? ") print("Here are the options for modifications: \n\ (1) Negate red \n\ (2) Negate blue\n\ (3) Negate green\n\ (4) Negate all\n\ (5) Remove red\n\ (6) Remove blue\n\ (7) Remove green\n\ (8) Flip horizontally\n\ (9) Grey scale\n") mod = input("What modification would you like to do? ") #add input validation loop here to ensure valid input from the user #open the file infile = open(inputFileName, 'r') #get the correct output file name, and open the file in write mode outputFileName = makeOutputFileName(inputFileName, mod) outfile = open(outputFileName, 'w') #process the entire file process_header(infile, outfile) process_body(infile, outfile, mod) #close both files, and let user know what the updated file is called infile.close() outfile.close() print("Your modified file has been created. It is called", outputFileName) #Uncomment this line once you are ready to test the full program #main()