-
Notifications
You must be signed in to change notification settings - Fork 9
/
ImageEditor.py
75 lines (57 loc) · 2.19 KB
/
ImageEditor.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
import PySimpleGUI as sg
import cv2
import numpy as np
def adjust_gamma(image, gamma=5.0):
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
def adjust_contrast(img, value):
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=value, tileGridSize=(8,8))
cl = clahe.apply(l)
limg = cv2.merge((cl,a,b))
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
return final
def main():
sg.theme('LightGrey')
layout = [
[sg.Text('Video Input', size=(60, 1), justification='center')],
[sg.Image(filename='', key='IMAGE')],
[sg.Text('Brightness', size=(15, 1), auto_size_text=False, justification='left'),
sg.Slider((1,100),
disable_number_display=True,
enable_events=True,
size=(50, 20),
orientation='h',
key='BRIGHTNESS',
default_value=10)],
[sg.Text('Contrast', size=(15, 1), auto_size_text=False, justification='left'),
sg.Slider((1,100),
disable_number_display=True,
enable_events=True,
size=(50, 20),
orientation='h',
key='CONTRAST',
default_value=5)],
]
window = sg.Window('Feed from webcam', layout, location=(800, 400))
cap = cv2.VideoCapture(0)
while True:
event, values = window.read(timeout=20)
if event == 'Exit' or event == sg.WIN_CLOSED:
break
ret, frame = cap.read()
if (frame.any() != None):
value_b = values['BRIGHTNESS']
value_b *= 0.1
if (value_b != 0):
frame = adjust_gamma(frame, gamma=value_b)
value_c = values['CONTRAST']
value_c *= 0.1
frame = adjust_contrast(frame, value_c)
imgbytes = cv2.imencode('.png', frame)[1].tobytes()
window['IMAGE'].update(data=imgbytes)
window.close()
main()