-
Notifications
You must be signed in to change notification settings - Fork 7
/
rgb_hist.py
122 lines (53 loc) · 1.62 KB
/
rgb_hist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
def getRed(redVal):
return '#%02x%02x%02x' % (redVal, 0, 0)
def getGreen(greenVal):
return '#%02x%02x%02x' % (0, greenVal, 0)
def getBlue(blueVal):
return '#%02x%02x%02x' % (0, 0, blueVal)
# Create an Image with specific RGB value
image = Image.open("3.png")
## Modify the color of two pixels
#
#image.putpixel((0,1), (1,1,5))
#
#image.putpixel((0,2), (2,1,5))
# Display the image
image.show()
# Get the color histogram of the image
histogram = image.histogram()
# Take only the Red counts
l1 = histogram[0:256]
# Take only the Blue counts
l2 = histogram[256:512]
# Take only the Green counts
l3 = histogram[512:768]
plt.figure(0)
plt.title(" Histogram Predicted Red Value of Image")
# R histogram
for i in range(0, 256):
plt.bar(i, l1[i], color = getRed(i), edgecolor=getRed(i), alpha=0.3)
# G histogram
plt.figure(1)
plt.title(" Histogram Predicted Green Value of Image")
for i in range(0, 256):
plt.bar(i, l2[i], color = getGreen(i), edgecolor=getGreen(i),alpha=0.3)
# B histogram
plt.figure(2)
plt.title(" Histogram Predicted Blue Value of Image")
for i in range(0, 256):
plt.bar(i, l3[i], color = getBlue(i), edgecolor=getBlue(i),alpha=0.3)
plt.show()
plt.figure(3)
histogram, bin_edges = np.histogram(image, bins=256, range=(0.0, 256))
plt.title(" Histogram Predicted RGB Image Values")
plt.xlabel(" value")
plt.ylabel("pixels")
plt.plot(bin_edges[0:-1], histogram) # <- or here