-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_thumbnails.py
More file actions
58 lines (48 loc) · 2.11 KB
/
make_thumbnails.py
File metadata and controls
58 lines (48 loc) · 2.11 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
#!/usr/bin/env python
import json
import os
from PIL import Image
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
import requests
# Load the JSON file
with open('data/adjusted_data.json', 'r') as f:
data = json.load(f)
# Function to process a single image
def process_image(hash_key, entry):
try:
# get the path with a GET to the server at port 3000 /hash-to-path/:hash
img_path = requests.get(f"http://localhost:3000/hash-to-path/{hash_key}").json()['path']
if not os.path.exists(img_path):
print(f"File {img_path} does not exist.")
return
img = Image.open(img_path).convert('RGB') # Ensure image is in RGB mode for JPEG
width = 400
if img.size[0] > width:
w_percent = (width / float(img.size[0]))
height = int((float(img.size[1]) * float(w_percent)))
img = img.resize((width, height), resample=Image.LANCZOS)
# Determine the output path based on the hash
# Use the first 3 characters of the hash for sharding
subdirs = [hash_key[0], hash_key[1], hash_key[2]]
output_dir = os.path.join('public', 'thumbnails', *subdirs)
# Ensure the directories exist
os.makedirs(output_dir, exist_ok=True)
# Use the rest of the hash as the filename with .jpg extension
output_filename = f"{hash_key[3:]}.jpg"
output_path = os.path.join(output_dir, output_filename)
# Save the thumbnail as JPEG
img.save(output_path, format='JPEG', quality=85)
except Exception as e:
print(f"Error processing image {hash_key}: {e}")
# Process images in threads
def main():
with ThreadPoolExecutor(max_workers=8) as executor:
futures = []
for hash_key, entry in data.items():
futures.append(executor.submit(process_image, hash_key, entry))
# Use tqdm to show progress bar
for future in tqdm(as_completed(futures), total=len(futures)):
pass # You can handle results here if needed
if __name__ == "__main__":
main()