Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- from PIL import Image
- def histogram(color):
- array = np.array(color).flatten()
- unique, counts = np.unique(array, return_counts=True)
- array_h = np.zeros(np.max(unique)+1)
- array_h[unique] = counts
- return array_h
- def grey_scale(red_c, green_c, blue_c):
- red_a = np.array(red_c)
- green_a = np.array(green_c)
- blue_a = np.array(blue_c)
- gray_scale_a = 0.299 * red_a + 0.587 * green_a + 0.114 * blue_a
- gray_scale_a = gray_scale_a.astype(int)
- return Image.fromarray(gray_scale_a)
- image = Image.open("img.jpg")
- red, green, blue = image.split()
- grey = grey_scale(red, green, blue)
- red_h = histogram(red)
- green_h = histogram(green)
- blue_h = histogram(blue)
- grey_h = histogram(grey)
- fig, ax = plt.subplots(4)
- ax[0].set_title("Red Histogram")
- ax[0].plot(red_h, '-r')
- ax[1].set_title("Green Histogram")
- ax[1].plot(green_h, '-g')
- ax[2].set_title("Blue Histogram")
- ax[2].plot(blue_h, '-b')
- ax[3].set_title("Grey Scale Histogram")
- ax[3].plot(grey_h, '-g')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement