File size: 1,691 Bytes
5763280 6f57a6d |
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 |
#!/usr/bin/env python
import os
import json
from PIL import Image
def get_image_info(image_path):
with Image.open(image_path) as img:
width, height = img.size
return width, height
def process_directory(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
image_path = os.path.join(root, file)
width, height = get_image_info(image_path)
base_name, ext = os.path.splitext(file)
blurhash_file = os.path.join(root, f"{base_name}{ext}.bh")
caption_file = os.path.join(root, f"{base_name}.caption")
blurhash = None
caption = None
if os.path.exists(blurhash_file):
with open(blurhash_file, 'r') as bh_file:
blurhash = bh_file.read().strip()
if os.path.exists(caption_file):
with open(caption_file, 'r') as cap_file:
caption = cap_file.read().strip()
json_data = {
"filename": file,
"width": width,
"height": height,
"blurhash": blurhash,
"caption": caption
}
json_file = os.path.join(root, f"{base_name}{ext}.json")
with open(json_file, 'w') as jf:
json.dump(json_data, jf, indent=4)
if __name__ == "__main__":
process_directory(os.getcwd())
|