-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_images.py
61 lines (47 loc) · 1.59 KB
/
prepare_images.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
import shutil
import subprocess
from pathlib import Path
import xxhash
NEW_IMAGES_PATH = Path('D:/desktop/new_images')
BG_FOLDER = Path('public/bg')
TEMP_FOLDER = Path('temp')
def main(path=NEW_IMAGES_PATH):
"""
Very clunky. Rewrite this in JS, importing the Squoosh library directly.
Also resize larger images to 1920 max
Better duplicate image detection
:param path:
:return:
"""
if TEMP_FOLDER.exists():
raise Exception(f'{TEMP_FOLDER} already exists')
if not NEW_IMAGES_PATH.exists():
raise Exception(f'{NEW_IMAGES_PATH} does not exist')
TEMP_FOLDER.mkdir()
hashes = set()
current_filename = 1
# Clear bg folder
for pic in BG_FOLDER.iterdir():
pic.unlink()
print(f'Removed {pic.name}')
# Iterate through new images and convert
for pic in NEW_IMAGES_PATH.iterdir():
# Calculate file hash
with pic.open('rb') as f:
hash = xxhash.xxh3_64_hexdigest(f.read())
if hash in hashes:
print(f'Duplicate image: {pic.name}')
continue
hashes.add(hash)
print(f'{pic} added ({hash})')
subprocess.call([r'C:\Program Files\nodejs\squoosh-cli.cmd',
'--mozjpeg', '{}',
'-d', TEMP_FOLDER.absolute(),
pic
])
print(f'Converted {pic}')
shutil.move(TEMP_FOLDER / f'{pic.stem}.jpg', BG_FOLDER / f'{current_filename}.jpg')
current_filename += 1
shutil.rmtree(TEMP_FOLDER)
if __name__ == '__main__':
main(NEW_IMAGES_PATH)