Advertisement
bensimmo

Representing Data with Images and Sound 2.9

Jan 21st, 2021
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. from PIL import Image    #Import the Image module from the PIL library
  2.  
  3. image = Image.open('image.png')    #Create a path to the image file
  4.  
  5. rgb_image = image.convert('RGB')    #Convert the image to RGB
  6.  
  7. width, height = image.size     #Assign the image's width and height to variables
  8.  
  9. lighten_image = Image.new('RGB', (width,height))     #Create a grid for the output image
  10. sepia_image = Image.new('RGB', (width,height))
  11.  
  12. #Set up loops to modify each pixel within the image
  13. for row in range(height):
  14.     for col in range(width):
  15.         r, g, b = rgb_image.getpixel((col, row))
  16.          # a lighten filter
  17.         lighten_image.putpixel((col, row), (min(255, r * 2),min(255, g * 2),min(255, b * 2) ))
  18.          # a sepia filter
  19.         sepia_image.putpixel((col, row),
  20.                               (min(255, int((0.393 * r) + (0.769 * g) + (0.189 * b))),
  21.                                min(255, int((0.349 * r) + (0.686 * g) + (0.168 * b))),
  22.                                min(255, int((0.272 * r) + (0.534 * g) + (0.131 * b)))
  23.                                ))
  24.        
  25. lighten_image.save("lightened.png")
  26. sepia_image.save("sepia.png")
  27.  
  28. lighten_image.show()
  29. sepia_image.show()
  30.  
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement