-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessImages.py
More file actions
executable file
·175 lines (131 loc) · 4.15 KB
/
processImages.py
File metadata and controls
executable file
·175 lines (131 loc) · 4.15 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Resize images and copy them to the static folder
"""
import os, sys
import json
import shutil
from PIL import Image
import numpy as np
import urllib.parse
# for getting dominany color
# from colorthief import ColorThief
IMAGECUTOFF = 100
try:
os.makedirs('artworks-json')
except FileExistsError:
pass
# PARSE IMAGES
root = 'artworks'
filetypes = ['png', 'jpg', 'jpeg', 'webp']
images = {}
imagecounter = {}
for path, subdirs, files in os.walk(root):
for name in files:
ending = name.split(".")[-1]
if not ending in filetypes:
continue
cat = path.split("/")[1]
img_path = os.path.join(path, name)
mod_date = int(os.path.getmtime(img_path))
try:
images[cat].append( (img_path, mod_date) )
except:
images[cat] = [ (img_path, mod_date) ]
for cat in images.keys():
# sort by modification date
images[cat].sort(key=lambda x: int(x[1]), reverse=True)
# only process the latest x = IMAGECUTOFF images for each category
if IMAGECUTOFF and cat != 'redteam':
images[cat] = images[cat][0:IMAGECUTOFF]
outfile = f'artworks-json/{cat}.json'
with open(outfile, 'w') as f:
json.dump(images[cat], f)
print(f"({len(images[cat])})\t{cat}: {outfile}")
# special processing for the latest images (because there is no folder for them)
# get latest 50 images
latest = []
for cat in images.keys():
for img in images[cat]:
if cat == 'redteam': continue
latest.append(img)
latest.sort(key=lambda x: int(x[1]), reverse=True)
latest = latest[0:49]
with open(f'artworks-json/latest.json', 'w') as f:
json.dump(latest, f)
def rgb_to_hex(r, g, b):
return ('{:X}{:X}{:X}').format(r, g, b)
# PROCESS IMAGES
root = 'artworks-json'
filetypes = ['json']
outroot = 'public'
chunkspath = 'public/artworks/json'
try:
os.makedirs(chunkspath)
except FileExistsError:
pass
for path, subdirs, files in os.walk(root):
imagecounter = 0
for name in files:
jsonfile = os.path.join(path, name)
imgs = json.load(open(jsonfile, 'r'))
convertedimgs = []
for info in imgs:
imgpath = info[0]
if name == 'latest.json':
cat = 'latest'
else:
cat = imgpath.split('/')[1]
outpath = os.path.join(outroot, imgpath)
webpoutpath = outpath.split('.')
webpoutpath[-1] = "webp"
webpoutpath = ".".join(webpoutpath)
im = Image.open(imgpath)
# resize with aspect ratio
w, h = im.size
ar = w/h
width = 400
height = int(400/ar)
try:
average_color_row = np.average(im, axis=0)
average_color = np.average(average_color_row, axis=0)
average_color = [ int(x) for x in average_color ]
average_color = "#" + rgb_to_hex(average_color[0], average_color[1], average_color[2])
except:
average_color = "#" + rgb_to_hex(222, 222, 222)
# cache check - skip if file already exists
if not os.path.isfile(webpoutpath):
im400 = im.resize((width,height))
try:
os.makedirs(os.path.dirname(outpath))
except FileExistsError:
pass
im400.save(webpoutpath, "webp")
# encode e.g. spaces and hashtags in filename
webpoutpath = urllib.parse.quote(webpoutpath)
print(webpoutpath)
# remove 'public/' from output path
webpoutpath = webpoutpath.split('/')[1:]
# encode e.g. hashtags and spaces
webpoutpath = "https://www.jonaso.de/" + "/".join(webpoutpath)
convertedimgs.append((webpoutpath, width, height, average_color))
# write json with webp
json.dump(convertedimgs, open(os.path.join(outroot, 'artworks', 'json', f'webp-{cat}.json'), 'w'))
chunksize = 100
chunks = [convertedimgs[x:x+100] for x in range(0, len(convertedimgs), 100)]
total = len(convertedimgs)
k = 0
for batch in chunks:
# detect last iteration
outbatch = {
'total': total,
'items': batch,
}
if k == len(chunks) - 1:
outbatch['next'] = False
else:
outbatch['next'] = k+1
json.dump(outbatch, open(os.path.join(chunkspath, f'webp-{cat}-{k}.json'), 'w'))
k += 1
shutil.rmtree('artworks-json', ignore_errors=True)