-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_image.py
executable file
·163 lines (104 loc) · 4.67 KB
/
color_image.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
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 3 01:57:31 2022
@author: Sam Iacavazzi
"""
from PIL import Image, ImageEnhance
import label_handler as lh
import image_correction as ic
import index_maker as im
import numpy as np
output = "output"
format = "jpg"
border = True
last = None
folder = "test"
"""
ORDER OF COLORS ***
1 ch: B&W
2 ch: red, blue *** fake green may be inserted into the middle
3 ch: red, green, blue
"""
triband_from_dualband = True
buffer = []
# BUG: Image order is incorrect sometimes
def build_mosaic(img):
if len(buffer) > 4:
buffer.pop(0)
if len(buffer) >= 1:
buffer.append(img)
composite = buffer[0]
for image in buffer:
composite = Image.alpha_composite(composite, image)
return composite
else:
buffer.insert(0,img)
return img
def export_sequence(camera, filters, target, delta, folder, mosaic, start_loc):
mask = Image.open("mask.png").convert("L")
index = im.get_images(camera, target, delta, filters, start_loc,make_csv=False)
num_channels = len(filters)
last = None
for i in range(0, len(index)):
imgs = []
lbls = []
try:
if num_channels == 1:
image = lh.label(index[filters[0]].iloc[i]).auto_center(border)
image = Image.fromarray(image)
else:
for filter in filters:
img = lh.label(index[filter].iloc[i], as_png = True)
imgs.append(img)
lbl = imgs[0]
for j in range(0, len(imgs)):
imgs[j] = imgs[j].resize_image()
try:
imgs = ic.align_images(imgs)
except:
print("Image alignment failure")
for j in range(0, len(imgs)):
imgs[j] = Image.fromarray(imgs[j]).convert('L')
if num_channels == 3:
image = Image.merge("RGBA",(imgs[2],imgs[1],imgs[0], mask))
image = np.asarray(image)
image = lbl.set_color(image).auto_center()
image = Image.fromarray(image)
if mosaic:
image = build_mosaic(image)
"""
if last is None:
last = image
else:
#image = ic.match_hist(last, image)
###
#image = np.asarray(image)
last = np.asarray(last)
r,g,b,a = image.split()
a = a.filter(ImageFilter.BLUR)
#alpha = np.asarray(alpha)
image = Image.merge("RGBA", (r,g,b,a))
image = np.asarray(image)
image = ic.imgAlign(last, image, True)
img = Image.fromarray(image)
last = Image.fromarray(last)
###
image = Image.alpha_composite(last, image)
last = image
"""
else:
if triband_from_dualband:
blank = Image.open("blank2.jpg").convert('L')
green = Image.merge("RGB",(imgs[0],blank,imgs[1])).convert('L')
image = Image.merge("RGB",(imgs[0],green,imgs[1]))
else:
image = Image.merge("P",imgs[0],imgs[1])
id = index["seconds_1970"].iloc[i]
title = f"{id}_{target}"
#image = Image.fromarray(image)
enhancer = ImageEnhance.Sharpness(image)
image = enhancer.enhance(1.5)
image.save(f"output/{folder}/{title}.png")
except Exception as e:
print(e)
continue