Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from PIL import Image
  4.  
  5.  
  6. def histogram(color):
  7. array = np.array(color).flatten()
  8. unique, counts = np.unique(array, return_counts=True)
  9. array_h = np.zeros(np.max(unique)+1)
  10. array_h[unique] = counts
  11. return array_h
  12.  
  13.  
  14. def grey_scale(red_c, green_c, blue_c):
  15. red_a = np.array(red_c)
  16. green_a = np.array(green_c)
  17. blue_a = np.array(blue_c)
  18. gray_scale_a = 0.299 * red_a + 0.587 * green_a + 0.114 * blue_a
  19. gray_scale_a = gray_scale_a.astype(int)
  20. return Image.fromarray(gray_scale_a)
  21.  
  22.  
  23. image = Image.open("img.jpg")
  24. red, green, blue = image.split()
  25. grey = grey_scale(red, green, blue)
  26.  
  27. red_h = histogram(red)
  28. green_h = histogram(green)
  29. blue_h = histogram(blue)
  30. grey_h = histogram(grey)
  31.  
  32.  
  33. fig, ax = plt.subplots(4)
  34. ax[0].set_title("Red Histogram")
  35. ax[0].plot(red_h, '-r')
  36. ax[1].set_title("Green Histogram")
  37. ax[1].plot(green_h, '-g')
  38. ax[2].set_title("Blue Histogram")
  39. ax[2].plot(blue_h, '-b')
  40. ax[3].set_title("Grey Scale Histogram")
  41. ax[3].plot(grey_h, '-g')
  42. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement