bensimmo

Representing Data with Images and Sound 2.10

Jan 21st, 2021
460
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. new_image = Image.new('RGB', (width,height))     #Create a grid for the output image
  10. new2_image = Image.new('RGB', (width,height))
  11. new3_image = Image.new('RGB', (width,height))
  12. grey_image = Image.new('RGB', (width,height))
  13. grey2_image = Image.new('RGB', (width,height))
  14. grey3_image = Image.new('RGB', (width,height))
  15.  
  16. #Set up loops to modify each pixel within the image
  17. for row in range(height):
  18.     for col in range(width):
  19.         r, g, b = rgb_image.getpixel((col, row))
  20.          # a black white filter
  21.         bw =round( max(r,g,b)  /255) *255
  22.         bw2=round( ((r+g+b)/3) /255) *255
  23.         bw3=round( min(r,g,b)  /255) *255
  24.         gr =max(r,g,b)
  25.         gr2=round( (r+g+b)/3)
  26.         gr3=min(r,g,b)
  27.        
  28.         new_image.putpixel(   (col, row), (bw,bw,bw) )
  29.         new2_image.putpixel(  (col, row), (bw2,bw2,bw2) )
  30.         new3_image.putpixel(  (col, row), (bw3,bw3,bw3) )
  31.         grey_image.putpixel(  (col, row), (gr,gr,gr) )
  32.         grey2_image.putpixel( (col, row), (gr2,gr2,gr2) )
  33.         grey3_image.putpixel( (col, row), (gr3,gr3,gr3) )
  34.  
  35.        
  36. #new_image.save("black-white.png")
  37.  
  38. new_image.show()
  39. new2_image.show()
  40. new3_image.show()
  41. grey_image.show()
  42. grey2_image.show()
  43. grey3_image.show()
  44.  
  45.  
RAW Paste Data