-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverager.py
411 lines (313 loc) · 14.4 KB
/
averager.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
"""Adaptive color average.
Average image colors in a pixel row until difference between averaged and next pixel in row reach threshold. Then repeat the same in column. Thus filter changes smooth image areas to completely flat colored, with detailed edges between them.
Standalone version for https://dnyarri.github.io/povthread.html
Preview part based on https://dnyarri.github.io/pypnm.html
History:
24.10.14.0 Initial version of filter template.
24.12.09.1 Finished rebuilding for standalone filter+GUI complex, with in-RAM PPM-based preview.
1.14.1.1 Image list moved to global to reduce rereading. Versioning harmonized with other programs.
"""
__author__ = 'Ilya Razmanov'
__copyright__ = '(c) 2024 Ilya Razmanov'
__credits__ = 'Ilya Razmanov'
__license__ = 'unlicense'
__version__ = '1.14.25.14'
__maintainer__ = 'Ilya Razmanov'
__email__ = '[email protected]'
__status__ = 'Production'
import array
from tkinter import Button, Frame, IntVar, Label, PhotoImage, Spinbox, Tk, filedialog
try:
import png
except ImportError:
print('Need PyPNG from : https://gitlab.com/drj11/pypng')
quit()
import png # PNG I/O: PyPNG from: https://gitlab.com/drj11/pypng
""" ┌─────────────────────────────┐
│ Image editing defined first │
└────-────────────────────────┘ """
def create_image(X: int, Y: int, Z: int) -> list[list[list[int]]]:
"""Create empty 3D nested list of X*Y*Z sizes"""
new_image = [[[0 for z in range(Z)] for x in range(X)] for y in range(Y)]
return new_image
def png2list(in_filename: str) -> tuple[int, int, int, int, list[list[list[int]]], dict]:
"""Take PNG filename and return PNG data in a human-friendly form."""
source = png.Reader(in_filename)
X, Y, pixels, info = source.asDirect() # Opening image, iDAT comes to "pixels" as bytearray
Z = info['planes'] # Channels number
if info['bitdepth'] == 8:
maxcolors = 255 # Maximal value for 8-bit channel
if info['bitdepth'] == 16:
maxcolors = 65535 # Maximal value for 16-bit channel
imagedata = tuple(pixels) # Creates tuple of bytes or whatever "pixels" generator returns
# Next part forcedly creates 3D list of int out of "imagedata" tuple of hell knows what
image3D = [[[int((imagedata[y])[(x * Z) + z]) for z in range(Z)] for x in range(X)] for y in range(Y)]
# List (image) of lists (rows) of lists (pixels) of ints (channels) created
return (X, Y, Z, maxcolors, image3D, info)
def list2png(out_filename: str, image3D: list[list[list[int]]], info: dict) -> None:
"""Take filename and image data in a suitable form, and create PNG file."""
# Determining list sizes
Y = len(image3D)
X = len(image3D[0])
Z = len(image3D[0][0])
# Overwriting "info" properties with ones determined from the list
info['size'] = (X, Y)
info['planes'] = Z
# flattening 3D list to 1D list for PNG .write_array method
image1D = [c for row in image3D for px in row for c in px]
# Writing PNG
resultPNG = open(out_filename, mode='wb')
writer = png.Writer(X, Y, **info)
writer.write_array(resultPNG, image1D)
resultPNG.close() # Close output
return None
def list2bin(list_3d: list[list[list[int]]], maxcolors: int, show_chessboard: bool = False) -> bytes:
"""Convert nested image data list to PGM P5 or PPM P6 (binary) data structure in memory."""
def _chess(x: int, y: int) -> int:
return int(maxcolors * 0.8) if ((y // 8) % 2) == ((x // 8) % 2) else maxcolors
# Determining list sizes
Y = len(list_3d)
X = len(list_3d[0])
Z = len(list_3d[0][0])
if Z < 3:
magic = 'P5'
else:
magic = 'P6'
if Z == 3 or Z == 1:
Z_READ = Z
list_1d = [list_3d[y][x][z] for y in range(Y) for x in range(X) for z in range(Z_READ)]
else:
Z_READ = Z - 1
if show_chessboard:
list_1d = [(((list_3d[y][x][z] * list_3d[y][x][Z_READ]) + (_chess(x, y) * (maxcolors - list_3d[y][x][Z_READ]))) // maxcolors) for y in range(Y) for x in range(X) for z in range(Z_READ)]
else:
list_1d = [list_3d[y][x][z] for y in range(Y) for x in range(X) for z in range(Z_READ)]
if maxcolors < 256:
datatype = 'B'
else:
datatype = 'H'
header = array.array('B', f'{magic}\n{X} {Y}\n{maxcolors}\n'.encode('ascii'))
content = array.array(datatype, list_1d)
content.byteswap()
return header.tobytes() + content.tobytes()
def filter(sourceimage: list[list[list[int]]], threshold_x: int, threshold_y: int) -> list[list[list[int]]]:
"""Average image pixels in a row until borderline threshold met, then repeat in a column.
Be careful: it works with RGB, so RGBA need to be fixed when saving!"""
# Determining list sizes
Y = len(sourceimage)
X = len(sourceimage[0])
Z = len(sourceimage[0][0])
# Creating empty intermediate image
medimage = create_image(X, Y, 3)
# Creating empty final image
resultimage = create_image(X, Y, 3)
for y in range(0, Y, 1):
if Z > 2:
r_sum, g_sum, b_sum = r, g, b = sourceimage[0][0][0:3]
else:
channel = sourceimage[0][0][0]
r_sum, g_sum, b_sum = r, g, b = channel, channel, channel
x_start = 0
number = 1
for x in range(0, X, 1):
if Z > 2:
r, g, b = sourceimage[y][x][0:3]
else:
channel = sourceimage[y][x][0]
r, g, b = channel, channel, channel
number += 1
r_sum += r
g_sum += g
b_sum += b
if (abs(r - (r_sum / number)) > threshold_x) or (abs(g - (g_sum / number)) > threshold_x) or (abs(b - (b_sum / number)) > threshold_x) or x == X:
for i in range(x_start, x - 1, 1):
medimage[y][i] = [int(r_sum / number), int(g_sum / number), int(b_sum / number)]
medimage[y][x] = [r, g, b]
x_start = x
number = 1
r_sum, g_sum, b_sum = r, g, b
else:
medimage[y][x] = [r, g, b]
for x in range(0, X, 1):
r_sum, g_sum, b_sum = r, g, b = medimage[0][0]
y_start = 0
number = 1
for y in range(0, Y, 1):
r, g, b = medimage[y][x]
number += 1
r_sum += r
g_sum += g
b_sum += b
if (abs(r - (r_sum / number)) > threshold_y) or (abs(g - (g_sum / number)) > threshold_y) or (abs(b - (b_sum / number)) > threshold_y) or x == X:
for i in range(y_start, y - 1, 1):
resultimage[i][x] = [int(r_sum / number), int(g_sum / number), int(b_sum / number)]
resultimage[y][x] = [r, g, b]
y_start = y
number = 1
r_sum, g_sum, b_sum = r, g, b
else:
resultimage[y][x] = [r, g, b]
return resultimage
""" ┌────────────┐
│ GUI events │
└────-───────┘ """
def DisMiss():
"""Kill dialog and continue"""
sortir.destroy()
def GetSource():
"""Opening source image and redefining other controls state"""
global zoom_factor, sourcefilename, preview, preview_data
global X, Y, Z, maxcolors, image3D, info
zoom_factor = 1
sourcefilename = filedialog.askopenfilename(title='Open image file to filter', filetypes=[('PNG', '.png')])
if sourcefilename == '':
return
X, Y, Z, maxcolors, image3D, info = png2list(sourcefilename)
preview_data = list2bin(image3D, maxcolors, True)
preview = PhotoImage(data=preview_data)
preview = preview.zoom(zoom_factor, zoom_factor) # "zoom" zooms in, "subsample" zooms out
zanyato.config(text='Source', image=preview, compound='top')
# enabling zoom
label_zoom.config(state='normal')
butt_plus.config(state='normal', cursor='hand2')
# updating zoom factor display
label_zoom.config(text=f'Zoom {zoom_factor}:1')
# disabling "Save as..." from previous image session, if any
butt89.config(state='disabled')
# enabling "Threshold" spins and their labels
spin01.config(state='normal')
spin02.config(state='normal')
info01.config(state='normal')
info02.config(state='normal')
# enabling "Filter" button
butt02.config(state='normal', cursor='hand2')
# enabling zoom
label_zoom.config(state='normal')
butt_plus.config(state='normal', cursor='hand2')
# updating zoom factor display
label_zoom.config(text=f'Zoom {zoom_factor}:1')
def RunFilter():
"""Filtering image, and previewing"""
global preview, preview_data
global X, Y, Z, maxcolors, image3D, info
# filtering part
threshold_x = maxcolors * int(spin01.get()) / 255 # Rescaling for 16-bit
threshold_y = maxcolors * int(spin02.get()) / 255
image3D = filter(image3D, threshold_x, threshold_y)
# preview result
preview_data = list2bin(image3D, maxcolors)
preview = PhotoImage(data=preview_data)
preview = preview.zoom(zoom_factor, zoom_factor) # "zoom" zooms in, "subsample" zooms out
zanyato.config(text='Result', image=preview, compound='top')
# enabling zoom
label_zoom.config(state='normal')
butt_plus.config(state='normal', cursor='hand2')
# updating zoom factor display
label_zoom.config(text=f'Zoom {zoom_factor}:1')
# enabling "Save as..."
butt89.config(state='normal', cursor='hand2')
def SaveAs():
"""Once pressed on Save as"""
global X, Y, Z, maxcolors, image3D, info
# Open "Save as..." file
savefilename = filedialog.asksaveasfilename(
title='Save filtered image',
filetypes=[('PNG', '.png')],
defaultextension=('PNG file', '.png'),
)
if savefilename == '':
return
# Forcing RGB on export
info['greyscale'] = False
info['planes'] = 3
info['alpha'] = False
if 'palette' in info:
del info['palette'] # images get promoted to smooth color when editing
if 'background' in info:
del info['background']
list2png(savefilename, image3D, info)
def zoomIn():
global zoom_factor, preview
zoom_factor = min(zoom_factor + 1, 3) # max zoom 3
preview = PhotoImage(data=preview_data)
preview = preview.zoom(zoom_factor, zoom_factor)
zanyato.config(image=preview)
# updating zoom factor display
label_zoom.config(text=f'Zoom {zoom_factor}:1')
# reenabling +/- buttons
butt_minus.config(state='normal', cursor='hand2')
if zoom_factor == 3: # max zoom 3
butt_plus.config(state='disabled', cursor='arrow')
else:
butt_plus.config(state='normal', cursor='hand2')
def zoomOut():
global zoom_factor, preview
zoom_factor = max(zoom_factor - 1, 1) # min zoom 1
preview = PhotoImage(data=preview_data)
preview = preview.zoom(zoom_factor, zoom_factor)
zanyato.config(image=preview)
# updating zoom factor display
label_zoom.config(text=f'Zoom {zoom_factor}:1')
# reenabling +/- buttons
butt_plus.config(state='normal', cursor='hand2')
if zoom_factor == 1: # min zoom 1
butt_minus.config(state='disabled', cursor='arrow')
else:
butt_minus.config(state='normal', cursor='hand2')
""" ╔══════════╗
║ Mainloop ║
╚══════════╝ """
sortir = Tk()
zoom_factor = 1
sortir.title('Averager')
sortir.geometry('+200+100')
sortir.minsize(300, 110)
# Main dialog icon is PPM as well!
icon = PhotoImage(data=b'P6\n2 2\n255\n\xff\x00\x00\xff\xff\x00\x00\x00\xff\x00\xff\x00')
sortir.iconphoto(True, icon)
frame_left = Frame(sortir, borderwidth=2, relief='groove')
frame_left.pack(side='left', anchor='nw')
frame_right = Frame(sortir, borderwidth=2, relief='groove')
frame_right.pack(side='left', anchor='nw')
""" ┌────────────┐
│ Left frame │
└───────────-┘ """
# Open image
butt01 = Button(frame_left, text='Open PNG...'.center(16, ' '), font=('helvetica', 16), cursor='hand2', justify='center', command=GetSource)
butt01.pack(side='top', padx=4, pady=[2, 12], fill='both')
# X-pass threshold control
info01 = Label(frame_left, text='Threshold X', font=('helvetica', 10), justify='left', state='disabled')
info01.pack(side='top', padx=4, pady=[10, 2], fill='both')
ini_threshold_x = IntVar(value=16)
spin01 = Spinbox(frame_left, from_=0, to=256, increment=1, textvariable=ini_threshold_x, state='disabled')
spin01.pack(side='top', padx=4, pady=[0, 2], fill='both')
# Y-pass threshold control
info02 = Label(frame_left, text='Threshold Y', font=('helvetica', 10), justify='left', state='disabled')
info02.pack(side='top', padx=4, pady=[0, 2], fill='both')
ini_threshold_y = IntVar(value=8)
spin02 = Spinbox(frame_left, from_=0, to=256, increment=1, textvariable=ini_threshold_y, state='disabled')
spin02.pack(side='top', padx=4, pady=[0, 2], fill='both')
# Filter start
butt02 = Button(frame_left, text='Filter', font=('helvetica', 16), cursor='arrow', justify='center', state='disabled', command=RunFilter)
butt02.pack(side='top', padx=4, pady=[0, 24], fill='both')
# Save result
butt89 = Button(frame_left, text='Save as...', font=('helvetica', 16), cursor='arrow', justify='center', state='disabled', command=SaveAs)
butt89.pack(side='top', padx=4, pady=2, fill='both')
# Exit
butt99 = Button(frame_left, text='Exit', font=('helvetica', 16), cursor='hand2', justify='center', command=DisMiss)
butt99.pack(side='bottom', padx=4, pady=2, fill='both')
""" ┌─────────────┐
│ Right frame │
└────────────-┘ """
zanyato = Label(frame_right, text='Preview area', font=('helvetica', 10), justify='center', borderwidth=2, relief='groove')
zanyato.pack(side='top')
frame_zoom = Frame(frame_right, width=300, borderwidth=2, relief='groove')
frame_zoom.pack(side='bottom')
butt_plus = Button(frame_zoom, text='+', font=('courier', 8), width=2, cursor='arrow', justify='center', state='disabled', command=zoomIn)
butt_plus.pack(side='left', padx=0, pady=0, fill='both')
butt_minus = Button(frame_zoom, text='-', font=('courier', 8), width=2, cursor='arrow', justify='center', state='disabled', command=zoomOut)
butt_minus.pack(side='right', padx=0, pady=0, fill='both')
label_zoom = Label(frame_zoom, text=f'Zoom {zoom_factor}:1', font=('courier', 8), state='disabled')
label_zoom.pack(side='left', anchor='n', padx=2, pady=0, fill='both')
sortir.mainloop()