-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.py
61 lines (45 loc) · 1.94 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
import PySimpleGUI as sg
from PIL import Image
Image.LOAD_TRUNCATED_IMAGES = True
import sys
BAR_WIDTH = 1 # width of each bar
BAR_SPACING = 1 # space between each bar
EDGE_OFFSET = 3 # offset from the left edge for first bar
GRAPH_SIZE = DATA_SIZE = (200, 100) # size in pixels
def draw_graph(curgraph, histogram, grcolor):
max_histogram = max(histogram)
curgraph.erase()
curgraph.draw_line((0, 0), (256, 0))
for x in range(0, 256, 25):
curgraph.draw_line((x, -3), (x, 3)) # tick marks
if x != 0:
# numeric labels
curgraph.draw_text(str(x), (x, -10), color='black')
#print ("max_histogram value for ", grcolor, ": ", max_histogram, "len ", len(histogram))
for i in range(len(histogram)):
graph_value = (histogram[i] / max_histogram * GRAPH_SIZE[1])
curgraph.draw_rectangle(top_left=(i * BAR_SPACING + EDGE_OFFSET, graph_value),
bottom_right=(i * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0),
line_color=grcolor, fill_color=grcolor)
#curgraph.draw_text(text=graph_value, location=(i*BAR_SPACING+EDGE_OFFSET+25, graph_value+10))
def show_histogram(window, imgpath):
rgraph = window['-RGRAPH-'] # type: sg.Graph
ggraph = window['-GGRAPH-']
bgraph = window['-BGRAPH-']
try:
image = Image.open(imgpath)
r, g, b = image.split()
# print("red\n", r.histogram())
draw_graph(rgraph, r.histogram(), "red")
draw_graph(ggraph, g.histogram(), "green")
draw_graph(bgraph, b.histogram(), "blue")
except Exception as e:
print("Error opening this image/file")
pass
def clean_histogram(window):
rgraph = window['-RGRAPH-'] # type: sg.Graph
ggraph = window['-GGRAPH-']
bgraph = window['-BGRAPH-']
rgraph.erase()
ggraph.erase()
bgraph.erase()