Advertisement
Guest User

Sharpen (older version)

a guest
Jan 22nd, 2020
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. """
  2. File: sharpen.py
  3. Project 7.10
  4.  
  5. Defines and tests a function to sharpen an image.
  6. """
  7.  
  8. from images import Image
  9.  
  10. def sharpen(image, degree, threshold):
  11.     """Builds and returns a sharpened image.  Expects an image
  12.    and two integers (the degree to which the image should be sharpened and the
  13.    threshold used to detect edges) as arguments."""
  14.    
  15.     def getDiff(colorVal1, colorVal2):
  16.         """This auxiliary function calculates the difference between
  17.        two tuples."""
  18.         (r1, g1, b1) = colorVal1
  19.         (r2, g2, b2) = colorVal2
  20.        
  21.         average1 = (r1 + g1 + b1) // 3
  22.         average2 = (r2 + g2 + b2) // 3
  23.        
  24.         difference = average1 - average2
  25.        
  26.         return difference
  27.        
  28.     imageCopy = image.clone()            # Get a copy of the original image
  29.     height = imageCopy.getHeight()       # Get image height
  30.     width = imageCopy.getWidth()         # Get image width
  31.    
  32.     # Step through the pixels in the function to detect the edges
  33.     for y in range(height -1):
  34.         for x in range(1, width):
  35.             (r, g, b) = imageCopy.getPixel(x, y)   # Get the color of the current pixel
  36.             (rBottom, gBottom, bBottom) = image.getPixel(x, y + 1) # Get the color of the pixel underneath current
  37.             (rLeft, gLeft, bLeft) = image.getPixel(x - 1, y)       # Get the color of the pixel next to current
  38.  
  39.             # Compare the current pixel with the one underneath it.
  40.             if abs(getDiff((r, g, b), (rBottom, gBottom, bBottom))) > threshold:
  41.                 tempColor = [r + degree, g + degree, b + degree]  # Use a list to hold temporary colors, increased by degree
  42.                 bottom = [r - degree, g - degree, b - degree]     # Degree is subtracted here, to increase contrast
  43.  
  44.                 # Use a loop to check for out-of range colors:
  45.                 for index in range(len(tempColor)):
  46.                     if tempColor[index] > 255:
  47.                         tempColor[index] = 255
  48.                     if bottom[index] < 0:
  49.                         bottom[index] = 0
  50.                     tempColor[index] = tempColor[index] - bottom[index]
  51.  
  52.                 # Set the new color for the pixels
  53.                 imageCopy.setPixel(x, y, tuple(tempColor))
  54.            
  55.             # Compare the current pixel with the one to its left    
  56.             elif abs(getDiff((r, g, b), (rLeft, gLeft, gLeft))) > threshold:
  57.                 tempColor = [r + degree, g + degree, b + degree]
  58.                 left = [r - degree, g - degree, b - degree]
  59.  
  60.                 # Check for out-of-range colors
  61.                 for index in range(len(tempColor)):
  62.                     if tempColor[index] > 255:
  63.                         tempColor[index] = 255
  64.                     if left[index] < 0:
  65.                         left[index] = 0
  66.                     tempColor[index] = tempColor[index] - left[index]
  67.  
  68.  
  69.                 # Set the new color
  70.                 imageCopy.setPixel(x, y, tuple(tempColor))
  71.                                    
  72.     return imageCopy
  73.    
  74.  
  75. def main():
  76.     filename = input("Enter the image file name: ")
  77.     image = Image(filename)
  78.     newimage = sharpen(image, 20, 15)
  79.     newimage.draw()
  80.  
  81. if __name__ == "__main__":
  82.    main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement