-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.py
More file actions
320 lines (173 loc) · 5.05 KB
/
map.py
File metadata and controls
320 lines (173 loc) · 5.05 KB
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
# img_viewer.py
import PySimpleGUI as sg
import os.path
from PIL import Image
import math
import cmath
## resoulution handaling stuff
# n,m are coords of the box
# l, h are the length and height of a box
def getInd(n,m,w,h):
points = []
for i in range(w):
for j in range(h):
points.append((i + w*n, j + h*m))
return points
# give img and another of the same size
def Compress(im, newImg, R):
pixelMap = im.load()
W, H = im.size
Wn = int(W/R)
Hn = int(H/R)
pixelsNew = newImg.load()
for n in range(Wn):
for m in range(Hn):
points = getInd(n, m, R, R)
counter = 0
sumo = 0
sumt = 0
sumth = 0
for p in points:
pix = pixelMap[p]
if (pix == None or pix == (255,255,255)):
continue
sumo += pix[0]
sumt += pix[1]
sumth += pix[2]
counter += 1
if counter == 0:
counter = 1
# avergae out the color and app\y
sumo = int(sumo/counter)
sumt = int(sumt/counter)
sumth = int(sumth/counter)
val = (sumo, sumt, sumth)
for p in points:
pixelsNew[p] = val
return newImg
### complex number stuff
# Complex transformation
LW = 0
LH = 0
# Compute Function
def functionComp(z, eq):
try:
return (eval(eq), True)
except (ZeroDivisionError):
return (z, False)
def CompTrans(r,i,W,H,eq):
sR = 1.0/W
sC = 1.0/H
#make number
F = complex(r,i)
# center
F -= complex(W,H)/2
# adjusting scale
# z *= scale
z = complex(F.real * sR, F.imag * sC)
# do math
z, status = functionComp(z,eq)
# un scale + uncenter
n = round((z.real * W) + W/2)
m = round((z.imag * H) + H/2)
if (n < W and n >= 0 and m < H and m >= 0):
return (n, m, status)
# if its not within the screen dont show
return (n, m, False)
def transform(file, eq, R):
im = Image.open(file)
pixelMap = im.load()
W, H = im.size
# make new image (output)
img = Image.new(im.mode, im.size)
pixelsNew = img.load()
# set background
for i in range(W):
for j in range(H):
pixelsNew[i,j] = (255,255,255)
# transform
for i in range(W):
for j in range(H):
n, m, status = CompTrans(i, j, W, H, eq)
if status:
pixelsNew[n,m] = pixelMap[i,j]
# Compress
compressedImg = Image.new(im.mode, im.size)
Compress(img, compressedImg, R)
im.close()
img.close()
compressedImg.save("output.png")
compressedImg.close()
# First the window layout in 2 columns
file_list_column = [
[
sg.Text("Image Folder"),
sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
sg.FolderBrowse(),
],
[sg.Listbox(values=[], enable_events=True, size=(40, 20), key="-FILE LIST-")],
[sg.Text('Resoulution'), sg.Input(key='-IN-')],
[sg.Text('Function'), sg.InputText(key='-INPUT-')],
[sg.Button('Transform', enable_events=True, key='-TRANSFORM-',font=('Arial Bold', 12))]
]
# For now will only show the name of the file that was chosen
image_viewer_column = [
[sg.Text("Choose an image from list on left:")],
[sg.Text(size=(40, 1), key="-TOUT-")],
[sg.Image(key="-IMAGE-")],
]
# ----- Full layout -----
layout = [
[
sg.Column(file_list_column),
sg.VSeperator(),
sg.Column(image_viewer_column),
]
]
window = sg.Window("Image Transformer", layout, resizable=True)
# get file Directory
dir_path = os.path.dirname(os.path.realpath(__file__))
# Run the Event Loop
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
# Folder name was filled in, make a list of files in the folder
if event == "-FOLDER-":
folder = values["-FOLDER-"]
try:
# Get list of files in folder
file_list = os.listdir(folder)
except:
file_list = []
fnames = [
f
for f in file_list
if os.path.isfile(os.path.join(folder, f))
and f.lower().endswith((".png", ".jpeg"))
]
window["-FILE LIST-"].update(fnames)
# A file was chosen from the listbox
elif event == "-FILE LIST-":
try:
filename = os.path.join(
values["-FOLDER-"], values["-FILE LIST-"][0]
)
window["-TOUT-"].update(filename)
im = Image.open(filename)
im.save("temp.png", "PNG")
window["-IMAGE-"].update(filename="temp.png")
except:
pass
elif event == "-TRANSFORM-":
try:
func = values["-INPUT-"]
except:
funx = "z"
try:
res = int(values['-IN-'])
except:
res = 1
transform(filename, func, res)
window["-IMAGE-"].update(filename = dir_path + "/output.png")
window.close()