This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
histogram.py
72 lines (55 loc) · 2.17 KB
/
histogram.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
from matplotlib import pyplot
from os.path import basename
from os.path import splitext
from PIL import Image
def get_file_name(file_path):
# https://stackoverflow.com/a/678266/9157799
return splitext(basename(file_path))[0]
def tampilkan_histogram(r, g, b, gambar):
intensitas = list(range(256))
lebar_bar = 0.3
# https://stackoverflow.com/q/9304408/9157799
intensitas = [i-lebar_bar for i in intensitas]
# https://stackoverflow.com/q/14270391/9157799
pyplot.bar(intensitas, r, width=lebar_bar, color='r')
intensitas = [i+lebar_bar for i in intensitas]
pyplot.bar(intensitas, g, width=lebar_bar, color='g')
intensitas = [i+lebar_bar for i in intensitas]
pyplot.bar(intensitas, b, width=lebar_bar, color='b')
pyplot.title('Histogram ' + gambar)
pyplot.xlabel('Intensitas')
pyplot.ylabel('Kemunculan')
pyplot.legend(['R', 'G', 'B'])
pyplot.show()
def histogram(gambar):
GAMBAR = Image.open(gambar)
PIXEL = GAMBAR.load()
ukuran_horizontal = GAMBAR.size[0]
ukuran_vertikal = GAMBAR.size[1]
gambar_r = Image.new('RGB', (ukuran_horizontal, ukuran_vertikal))
pixel_r = gambar_r.load()
gambar_g = Image.new('RGB', (ukuran_horizontal, ukuran_vertikal))
pixel_g = gambar_g.load()
gambar_b = Image.new('RGB', (ukuran_horizontal, ukuran_vertikal))
pixel_b = gambar_b.load()
# https://stackoverflow.com/q/10712002/9157799
r = [0] * 256
g = [0] * 256
b = [0] * 256
for x in range(ukuran_horizontal):
for y in range(ukuran_vertikal):
intensitas_r = PIXEL[x, y][0]
intensitas_g = PIXEL[x, y][1]
intensitas_b = PIXEL[x, y][2]
r[intensitas_r] += 1
g[intensitas_g] += 1
b[intensitas_b] += 1
pixel_r[x, y] = (intensitas_r, 0, 0)
pixel_g[x, y] = (0, intensitas_g, 0)
pixel_b[x, y] = (0, 0, intensitas_b)
gambar_r.save('img/' + get_file_name(gambar) + '_r.jpg')
gambar_g.save('img/' + get_file_name(gambar) + '_g.jpg')
gambar_b.save('img/' + get_file_name(gambar) + '_b.jpg')
tampilkan_histogram(r, g, b, gambar)
histogram('img/gambar.jpg')
histogram('img/gambar2.jpg')