forked from plotly/dash-image-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
152 lines (118 loc) · 4.1 KB
/
utils.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import dash_core_components as dcc
import dash_html_components as html
import json
import plotly.graph_objs as go
import dash_reusable_components as drc
from PIL import Image, ImageFilter, ImageDraw, ImageEnhance
# [filename, image_signature, action_stack]
STORAGE_PLACEHOLDER = json.dumps({
'filename': None,
'image_signature': None,
'action_stack': []
})
IMAGE_STRING_PLACEHOLDER = drc.pil_to_b64(Image.open('images/default.jpg').copy(), enc_format='jpeg')
GRAPH_PLACEHOLDER = dcc.Graph(id='interactive-image', style={'height': '80vh'})
# Maps process name to the Image filter corresponding to that process
FILTERS_DICT = {
'blur': ImageFilter.BLUR,
'contour': ImageFilter.CONTOUR,
'detail': ImageFilter.DETAIL,
'edge_enhance': ImageFilter.EDGE_ENHANCE,
'edge_enhance_more': ImageFilter.EDGE_ENHANCE_MORE,
'emboss': ImageFilter.EMBOSS,
'find_edges': ImageFilter.FIND_EDGES,
'sharpen': ImageFilter.SHARPEN,
'smooth': ImageFilter.SMOOTH,
'smooth_more': ImageFilter.SMOOTH_MORE
}
ENHANCEMENT_DICT = {
'color': ImageEnhance.Color,
'contrast': ImageEnhance.Contrast,
'brightness': ImageEnhance.Brightness,
'sharpness': ImageEnhance.Sharpness
}
def generate_lasso_mask(image, selectedData):
"""
Generates a polygon mask using the given lasso coordinates
:param selectedData: The raw coordinates selected from the data
:return: The polygon mask generated from the given coordinate
"""
height = image.size[1]
y_coords = selectedData['lassoPoints']['y']
y_coords_corrected = [height - coord for coord in y_coords]
coordinates_tuple = list(zip(selectedData['lassoPoints']['x'], y_coords_corrected))
mask = Image.new('L', image.size)
draw = ImageDraw.Draw(mask)
draw.polygon(coordinates_tuple, fill=255)
return mask
def apply_filters(image, zone, filter, mode):
filter_selected = FILTERS_DICT[filter]
if mode == 'select':
crop = image.crop(zone)
crop_mod = crop.filter(filter_selected)
image.paste(crop_mod, zone)
elif mode == 'lasso':
im_filtered = image.filter(filter_selected)
image.paste(im_filtered, mask=zone)
def apply_enhancements(image, zone, enhancement, enhancement_factor, mode):
enhancement_selected = ENHANCEMENT_DICT[enhancement]
enhancer = enhancement_selected(image)
im_enhanced = enhancer.enhance(enhancement_factor)
if mode == 'select':
crop = im_enhanced.crop(zone)
image.paste(crop, box=zone)
elif mode == 'lasso':
image.paste(im_enhanced, mask=zone)
def show_histogram(image):
def hg_trace(name, color, hg):
line = go.Scatter(
x=list(range(0, 256)),
y=hg,
name=name,
line=dict(color=(color)),
mode='lines',
showlegend=False
)
fill = go.Scatter(
x=list(range(0, 256)),
y=hg,
mode='fill',
name=name,
line=dict(color=(color)),
fill='tozeroy',
hoverinfo='none'
)
return line, fill
hg = image.histogram()
if image.mode == 'RGBA':
rhg = hg[0:256]
ghg = hg[256:512]
bhg = hg[512:768]
ahg = hg[768:]
data = [
*hg_trace('Red', '#FF4136', rhg),
*hg_trace('Green', '#2ECC40', ghg),
*hg_trace('Blue', '#0074D9', bhg),
*hg_trace('Alpha', 'gray', ahg)
]
title = 'RGBA Histogram'
elif image.mode == 'RGB':
# Returns a 768 member array with counts of R, G, B values
rhg = hg[0:256]
ghg = hg[256:512]
bhg = hg[512:768]
data = [
*hg_trace('Red', '#FF4136', rhg),
*hg_trace('Green', '#2ECC40', ghg),
*hg_trace('Blue', '#0074D9', bhg),
]
title = 'RGB Histogram'
else:
data = [*hg_trace('Gray', 'gray', hg)]
title = 'Grayscale Histogram'
layout = go.Layout(
title=title,
margin=go.Margin(l=35, r=35),
legend=dict(x=0, y=1.15, orientation="h")
)
return go.Figure(data=data, layout=layout)