Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## December 7, 2019
- ## HW9 - Working with data files(again)
- ## Objectives:
- ## working with lists.
- ## continued practice:
- ## reading from and writing to files.
- ## designing with control structures and functions.
- ## This program will:
- ## read in data from the user's file input.
- ## create 2 new image files with the following characteristics:
- ## an image with all of the blue values 'negated.'
- ## a gray scale version of the image as well.
- ## Welcome Greeting:
- print('Image Color Converter')
- print('This program will open and read in data from the image file of choice.')
- print('It will also create 2 new image files-'
- '\n' '\t' 'First: Image that will negate all blue values.'
- '\n' '\t' 'Second: Grayscale version of the image.')
- ## [ I N P U T ]
- ## main()function will include:
- ## user prompt for file input.
- ## error messages.
- ## creating the new file with results.
- def main():
- tryAgain = True ## exit sentinel regarding about continuation
- while tryAgain == True:
- while True:
- try:
- ## prompting user file input.
- inputFile = input(str('Enter image file name to convert \
- (include \'.ppm\'): '))
- selectFile = open(inputFile, 'r') ## opening and read file.
- dataFile = selectFile.read()## storing inputFile data.
- rawArray = [] ## creating empty list to store dataFile elements
- ## placing each color value into its designated index
- rawArray = dataFile.split()
- maxColorValue = int(rawArray[3])
- ## creating empty list to store group of elements
- pixelArray = []
- for idx in range(4, len(rawArray) - 4):
- try:
- pixelArray.append(int(rawArray[idx]))
- except ValueError:
- pass
- blueImage = negateBlue(pixelArray, maxColorValue)
- ## calling outputFile() to create file.
- outputFile(blueImage, inputFile, rawArray, '_negate_blue.ppm')
- greyImage = greyScale(pixelArray, maxColorValue)
- outputFile(greyImage, inputFile, rawArray, '_greyscale.ppm')
- selectFile.close() ## closing current input file.
- ## ERROR MESSAGE: displays when the user enters incorrect file name
- ## or file path.
- except FileNotFoundError:
- print('Wrong file or file path. Please enter correct file!')
- print()
- print()
- else:
- break ## breaking out of the loop.
- yesno = True ## exit sentinel for wrong input.
- ## If user enter any value that is not a YES or NO, this will display
- ## the decision prompt again until they enter any of the correct value.
- while yesno == True:
- decision = input('Do you want to convert another file? Yes or No?')
- print()
- print()
- if decision.lower() == 'no':
- print('----- End of Program -----')
- print()
- print()
- ## Flag Variable - this will close the loop and end the program.
- tryagain = False
- ## Loop exits.
- yesno = False
- ## Displays message if the user does want to continue.
- ## This will also loop back to main().
- elif decision.lower() == 'yes':
- print()
- print()
- yesno = True
- ## Error message if user enters anything besides the YES/NO options.
- else:
- print('ERROR: Please enter YES or NO.')
- print()
- print()
- tryAgain = False
- ## outputFile() function will:
- ## create two NEW output file.
- def outputFile(pixelArray, inputFile, rawArray, extName):
- ## removes the .ppm or '.' extension in the original inputFile name.
- removedExtension = inputFile.replace('.ppm', '')
- ## opens and writes new file to log results.
- ## first outFile - negating blue
- outFile = open(str(removedExtension) + extName, 'w+')
- outFile.write(rawArray[0] + ' ')
- outFile.write(rawArray[1] + ' ')
- outFile.write(rawArray[2] + ' ')
- outFile.write(rawArray[3] + ' ')
- for idx in pixelArray:
- outFile.write(str(pixelArray[idx]) + ' ')
- ## changing pixels to opposite of blue value
- def negateBlue(pixelArray, maxColorValue):
- for idx in pixelArray:
- if idx % 3 == 2:
- pixelArray[idx] = 255 - pixelArray[idx]
- return pixelArray
- ## calculates rgb pixels and changes it to gray
- def greyScale(pixelArray, maxColorValue):
- index = 0 ## count
- while index + 3 < len(pixelArray):
- x = int((pixelArray[index] + pixelArray[index + 1] \
- + pixelArray[index + 2])/ 3)
- pixelArray[index] = x
- pixelArray[index + 1] = x
- pixelArray[index + 2] = x
- index += 3
- return pixelArray
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement