Advertisement
Guest User

i cry

a guest
Dec 8th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.35 KB | None | 0 0
  1. ## December 7, 2019
  2. ## HW9 - Working with data files(again)
  3.  
  4. ## Objectives:
  5.     ## working with lists.
  6.     ## continued practice:
  7.         ## reading from and writing to files.
  8.         ## designing with control structures and functions.
  9.  
  10. ## This program will:
  11.     ## read in data from the user's file input.
  12.     ## create 2 new image files with the following characteristics:
  13.         ## an image with all of the blue values 'negated.'
  14.         ## a gray scale version of the image as well.
  15.  
  16. ## Welcome Greeting:
  17. print('Image Color Converter')
  18. print('This program will open and read in data from the image file of choice.')
  19. print('It will also create 2 new image files-'
  20.       '\n' '\t' 'First: Image that will negate all blue values.'
  21.       '\n' '\t' 'Second: Grayscale version of the image.')
  22.  
  23. ## [ I N P U T ]
  24. ## main()function will include:
  25.     ## user prompt for file input.
  26.     ## error messages.
  27.     ## creating the new file with results.
  28. def main():
  29.  
  30.     tryAgain = True ## exit sentinel regarding about continuation
  31.  
  32.     while tryAgain == True:
  33.        
  34.         while True:
  35.             try:
  36.             ## prompting user file input.    
  37.                 inputFile = input(str('Enter image file name to convert \
  38.                (include \'.ppm\'): '))
  39.                 selectFile = open(inputFile, 'r') ## opening and read file.
  40.                 dataFile = selectFile.read()## storing inputFile data.
  41.                    
  42.  
  43.                 rawArray = [] ## creating empty list to store dataFile elements
  44.  
  45.                 ## placing each color value into its designated index
  46.  
  47.                 rawArray = dataFile.split()
  48.  
  49.                 maxColorValue = int(rawArray[3])
  50.                
  51.                 ## creating empty list to store group of elements
  52.                 pixelArray = []
  53.                
  54.                 for idx in range(4, len(rawArray) - 4):
  55.                    
  56.                     try:
  57.                         pixelArray.append(int(rawArray[idx]))
  58.                     except ValueError:
  59.                         pass
  60.                
  61.                 blueImage = negateBlue(pixelArray, maxColorValue)
  62.                
  63.                 ## calling outputFile() to create file.
  64.                 outputFile(blueImage, inputFile, rawArray, '_negate_blue.ppm')
  65.                
  66.  
  67.                 greyImage = greyScale(pixelArray, maxColorValue)
  68.                 outputFile(greyImage, inputFile, rawArray, '_greyscale.ppm')
  69.  
  70.                
  71.                 selectFile.close() ## closing current input file.
  72.             ## ERROR MESSAGE: displays when the user enters incorrect file name
  73.             ## or file path.
  74.             except FileNotFoundError:
  75.                 print('Wrong file or file path. Please enter correct file!')
  76.                 print()
  77.                 print()
  78.             else:
  79.                 break ## breaking out of the loop.
  80.  
  81.         yesno = True ## exit sentinel for wrong input.
  82.         ## If user enter any value that is not a YES or NO, this will display
  83.         ## the decision prompt again until they enter any of the correct value.
  84.         while yesno == True:
  85.             decision = input('Do you want to convert another file? Yes or No?')
  86.             print()
  87.             print()
  88.  
  89.             if decision.lower() == 'no':
  90.                 print('----- End of Program -----')
  91.                 print()
  92.                 print()
  93.                 ## Flag Variable - this will close the loop and end the program.
  94.                 tryagain = False
  95.                 ## Loop exits.
  96.                 yesno = False
  97.                
  98.             ## Displays message if the user does want to continue.
  99.             ## This will also loop back to main().
  100.             elif decision.lower() == 'yes':
  101.                 print()
  102.                 print()
  103.                 yesno = True
  104.                
  105.             ## Error message if user enters anything besides the YES/NO options.
  106.             else:
  107.                 print('ERROR: Please enter YES or NO.')
  108.                 print()
  109.                 print()
  110.         tryAgain = False
  111.  
  112.  
  113. ## outputFile() function will:
  114.     ## create two NEW output file.
  115. def outputFile(pixelArray, inputFile, rawArray, extName):
  116.     ## removes the .ppm or '.' extension in the original inputFile name.
  117.     removedExtension = inputFile.replace('.ppm', '')
  118.  
  119.     ## opens and writes new file to log results.
  120.     ## first outFile - negating blue
  121.     outFile = open(str(removedExtension) + extName, 'w+')
  122.  
  123.    
  124.     outFile.write(rawArray[0] + ' ')
  125.     outFile.write(rawArray[1] + ' ')
  126.     outFile.write(rawArray[2] + ' ')
  127.     outFile.write(rawArray[3] + ' ')
  128.    
  129.     for idx in pixelArray:
  130.         outFile.write(str(pixelArray[idx]) + ' ')
  131.    
  132.                
  133. ## changing pixels to opposite of blue value
  134. def negateBlue(pixelArray, maxColorValue):
  135.     for idx in pixelArray:
  136.         if idx % 3 == 2:
  137.             pixelArray[idx] = 255 - pixelArray[idx]
  138.     return pixelArray
  139.  
  140. ## calculates rgb pixels and changes it to gray
  141. def greyScale(pixelArray, maxColorValue):
  142.  
  143.     index = 0 ## count
  144.  
  145.     while index + 3 < len(pixelArray):
  146.  
  147.         x = int((pixelArray[index] + pixelArray[index + 1] \
  148.              + pixelArray[index + 2])/ 3)
  149.  
  150.         pixelArray[index] = x
  151.         pixelArray[index + 1] = x
  152.         pixelArray[index + 2] = x
  153.  
  154.         index += 3
  155.  
  156.     return pixelArray
  157.  
  158. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement