ClearCode

Colors

May 18th, 2022
1,718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. from PIL import Image, ImageFilter
  2. from numpy import array
  3.  
  4. # image import
  5. image = Image.open('picture.jpg')
  6.  
  7. #################################
  8. # Analysing picture information #
  9. #################################
  10.  
  11. # numpy section
  12. # print(array(image))
  13. # print(array(image.getchannel('R')))
  14.  
  15. # colors only
  16. # print(image.getpixel((0,0)))
  17. # print(image.getcolors(maxcolors = image.size[0] * image.size[1])) # maxcolors argument = 256 -> if image has more colors it returns none
  18.  
  19. # getting picture information
  20. # print(image.mode)
  21. # print(image.getbands())
  22. # print(image.info)
  23.  
  24. # channels
  25. # red_channel = image.getchannel('R')
  26. # red_channel.show()
  27.  
  28. #####################
  29. # Color conversions #
  30. #####################
  31.  
  32. # 1 bit grayscale image
  33. # image_grayscale_1bit = image.convert('1')
  34. # print(array(image_grayscale_1bit, dtype = int))
  35. # print(image_grayscale_1bit.getbands())
  36. # image_grayscale_1bit.show()
  37.  
  38. # 8 bit grayscale
  39. # grayscale_l = image.convert('L')
  40. # print(array(grayscale_l))
  41. # print(grayscale_l.getbands())
  42. # grayscale_l.show()
  43.  
  44. # Palette
  45. # palette = image.convert('P') # 256 colors only
  46. # print(array(palette)) # not colors! references to a color in a palette
  47. # print(palette.getpalette())
  48. # print(array(palette.getpalette()).reshape(256,3))
  49.  
  50. # palette_16 = image.convert('P',palette = Image.Palette.ADAPTIVE, colors = 16)
  51.  
  52. # update the color palette
  53. # new_palette = [x // 2 for x in palette_16.getpalette()]
  54. # palette_16.putpalette(new_palette)
  55. # palette_16.show()
  56.  
  57. ############################
  58. # change individual pixels #
  59. ############################
  60.  
  61. # image.putpixel((100,200),(255,0,0))
  62. for x in range(image.size[0]):
  63.     for y in range(image.size[1]):
  64.         if image.getpixel((x,y))[0] > 200:
  65.             image.putpixel((x,y),(0,0,0))
  66. image.show()
Advertisement
Add Comment
Please, Sign In to add comment