#!/usr/bin/env python # -*- coding: utf-8 -*- 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())