-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.py
413 lines (311 loc) · 12.5 KB
/
main.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
412
413
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import base64
import io
import json
import logging
# pip install exifread
import exifread
import requests
from flask import Flask, jsonify, render_template_string, redirect, request
# pip install humanize
from humanize import naturalsize as sizeof_fmt
from PIL import Image
# SOURCE: https://github.com/gil9red/SimplePyScripts/blob/master/print_exif/main.py
def get_exif_tags(file_object_or_file_name, as_category=True):
if type(file_object_or_file_name) == str:
# Open image file for reading (binary mode)
file_object_or_file_name = open(file_object_or_file_name, mode="rb")
# Return Exif tags
tags = exifread.process_file(file_object_or_file_name)
tags_by_value = dict()
if not tags:
# print('Not tags')
return tags_by_value
# print('Tags ({}):'.format(len(tags)))
for tag, value in tags.items():
# Process value
try:
if value.field_type == 1:
try:
# If last 2 items equals [0, 0]
if value.values[-2:] == [0, 0]:
value = bytes(value.values[:-2]).decode("utf-16")
else:
value = bytes(value.values).decode("utf-16")
except:
value = str(value.values)
else:
value = value.printable
value = value.strip()
except:
# Example tag JPEGThumbnail
if type(value) == bytes:
value = base64.b64encode(value).decode()
# print(' "{}": {}'.format(tag, value))
if not as_category:
tags_by_value[tag] = value
else:
# Fill categories_by_tag
if " " in tag:
category, sub_tag = tag.split(" ", maxsplit=1)
if category not in tags_by_value:
tags_by_value[category] = dict()
tags_by_value[category][sub_tag] = value
else:
tags_by_value[tag] = value
# print()
return tags_by_value
# SOURCE: https://github.com/gil9red/SimplePyScripts/blob/master/img_to_base64_html/main.py
def img_to_base64_html(file_name__or__bytes__or__file_object):
arg = file_name__or__bytes__or__file_object
if type(arg) == str:
with open(arg, mode="rb") as f:
img_bytes = f.read()
elif type(arg) == bytes:
img_bytes = arg
else:
img_bytes = arg.read()
bytes_io = io.BytesIO(img_bytes)
img = Image.open(bytes_io)
img_base64 = base64.b64encode(img_bytes).decode("utf-8")
# print(img_base64)
return f"data:image/{img.format.lower()};base64,{img_base64}"
# SOURCE: https://github.com/gil9red/SimplePyScripts/blob/84651cfefaee768851170ec4ba7d025bbaae622d/get_image_info/main.py#L84
def get_image_info(file_name__or__bytes__or__bytes_io, pretty_json_str=False):
data = file_name__or__bytes__or__bytes_io
type_data = type(data)
# File name
if type_data == str:
with open(data, mode="rb") as f:
data = f.read()
if type(data) == bytes:
data = io.BytesIO(data)
length = len(data.getvalue())
exif = get_exif_tags(data)
img = Image.open(data)
info = dict()
info["length"] = dict()
info["length"]["value"] = length
info["length"]["text"] = sizeof_fmt(length)
info["format"] = img.format
info["mode"] = img.mode
info["channels"] = len(img.getbands())
info["bit_color"] = {
"1": 1,
"L": 8,
"P": 8,
"RGB": 24,
"RGBA": 32,
"CMYK": 32,
"YCbCr": 24,
"I": 32,
"F": 32,
}[img.mode]
info["size"] = dict()
info["size"]["width"] = img.width
info["size"]["height"] = img.height
info["exif"] = exif
if pretty_json_str:
info = json.dumps(info, indent=4, ensure_ascii=False)
return info
app = Flask(__name__)
# http://flask.pocoo.org/docs/0.12/config/#config
# By default Flask will serialize JSON objects in a way that the keys are ordered. This is done in order to
# ensure that independent of the hash seed of the dictionary the return value will be consistent to not trash external
# HTTP caches. You can override the default behavior by changing this variable. This is not recommended but might give
# you a performance improvement on the cost of cacheability.
app.config["JSON_SORT_KEYS"] = False
logging.basicConfig(level=logging.DEBUG)
@app.route("/")
def index():
return render_template_string(
"""\
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
<title>get_upload_image_info</title>
<script type="text/javascript" src="{{ url_for('static', filename='js/jquery-3.1.1.min.js') }}"></script>
<style>
/*
https://stackoverflow.com/a/7220510/5909792
FOR function syntaxHighlight
*/
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; white-space:pre-wrap; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
</style>
</head>
<body>
<p>get_upload_image_info:</p>
<table>
<tr>
<td>
<form class="form__upload_file" action="/get_info_from_file" method="post" enctype="multipart/form-data">
<label>Image file: <input type="file" name="file" accept="image/*"></label>
<p><input type="submit"></p>
</form>
</td>
<td><div style="width: 100px;"></div></td>
<td>
<form class="form__upload_url" action="/get_info_from_url" method="post" enctype="multipart/form-data">
<label>Image url: <input type="url" name="url" onkeydown="this.style.width = ((this.value.length + 1) * 8) + 'px';"></label>
<p><input type="submit"></p>
</form>
</td>
</tr>
<table>
<div class="block progress" style="display: none">
<p>Пожалуйста, подождите, файл загружаются.</p>
<progress class="progress upload" max="100" value="0"></progress>
</div>
<div class="result json show" style="display: none">
<p>Результат от сервера:</p>
<img width="200px" height="200px" alt="image"/><br/><br/>
<pre></pre>
</div>
<script>
$(document).ready(function() {
function progress(e) {
if(e.lengthComputable) {
var max = e.total;
var current = e.loaded;
var percentage = (current * 100) / max;
console.log(percentage);
$('.progress.upload').val(percentage);
}
}
// https://stackoverflow.com/a/7220510/5909792
function syntaxHighlight(json) {
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
function on_success(data) {
console.log(data);
$('.block.progress').hide();
$('.result.json.show > img').attr('src', data.img_base64);
if (data.img_base64.length > 100) {
data.img_base64 = data.img_base64.substring(0, 100) + "...";
}
var json_str = JSON.stringify(data, undefined, 4);
console.log(json_str);
json_str = syntaxHighlight(json_str);
$('.result.json.show > pre').html(json_str);
$('.result.json.show').show();
}
$(".form__upload_file").submit(function() {
$('.block.progress').show();
$('.result.json.show').hide();
var thisForm = this;
var url = $(this).attr("action");
var method = $(this).attr("method");
if (method === undefined) {
method = "get";
}
// var data = $(this).serialize();
//
// For send file object:
var input = $(".form__upload_file > input[type=file]");
var data = new FormData(thisForm);
$.ajax({
url: url,
method: method, // HTTP метод, по умолчанию GET
data: data,
dataType: "json", // тип данных загружаемых с сервера
// Без этих опций неудастся передать файл
processData: false,
contentType: false,
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', progress, false);
}
return myXhr;
},
cache:false,
success: on_success,
});
return false;
});
$(".form__upload_url").submit(function() {
$('.block.progress').show();
$('.result.json.show').hide();
var thisForm = this;
var url = $(this).attr("action");
var method = $(this).attr("method");
if (method === undefined) {
method = "get";
}
var data = $(this).serialize();
// For send file object:
//// var input = $(".form__upload_file > input[type=file]");
//// var data = new FormData(thisForm);
$.ajax({
url: url,
method: method, // HTTP метод, по умолчанию GET
data: data,
dataType: "json", // тип данных загружаемых с сервера
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', progress, false);
}
return myXhr;
},
success: on_success,
});
return false;
});
});
</script>
</body>
</html>
"""
)
@app.route("/get_info_from_file", methods=["POST"])
def get_info_from_file():
print(request.files)
# check if the post request has the file part
if "file" not in request.files:
return redirect("/")
file = request.files["file"]
file_data = file.stream.read()
info = get_image_info(file_data)
info["img_base64"] = img_to_base64_html(file_data)
return jsonify(info)
@app.route("/get_info_from_url", methods=["POST"])
def get_info_from_url():
print(request.form)
if "url" not in request.form:
return redirect("/")
url = request.form["url"]
file_data = requests.get(url).content
info = get_image_info(file_data)
info["img_base64"] = img_to_base64_html(file_data)
return jsonify(info)
if __name__ == "__main__":
app.debug = True
# Localhost
# port=0 -- random free port
# app.run(port=0)
app.run(port=5000)
# # Public IP
# app.run(host='0.0.0.0')