agrocrop / image_utils.py
gkc55's picture
Fix image_utils.py to handle all images in single directory
6248155
raw
history blame contribute delete
1.28 kB
import base64
import os
def get_base64_images():
images = {}
static_dir = 'static/images'
try:
# Create static/images directory if it doesn't exist
os.makedirs(static_dir, exist_ok=True)
print(f"Looking for images in: {static_dir}")
# Load all image files in the directory
if os.path.exists(static_dir):
for image_file in os.listdir(static_dir):
if image_file.lower().endswith(('.png', '.jpg', '.jpeg')):
try:
with open(os.path.join(static_dir, image_file), 'rb') as f:
# Get filename without extension as the key
name = os.path.splitext(image_file)[0].lower()
images[name] = base64.b64encode(f.read()).decode()
print(f"Loaded: {image_file}")
except Exception as e:
print(f"Error loading {image_file}: {e}")
else:
print(f"Warning: Directory not found: {static_dir}")
except Exception as e:
print(f"Error in get_base64_images: {e}")
print(f"Current working directory: {os.getcwd()}")
return images