Fix image_utils.py to handle all images in single directory
Browse files- image_utils.py +27 -16
image_utils.py
CHANGED
|
@@ -1,22 +1,33 @@
|
|
| 1 |
-
import base64
|
| 2 |
import os
|
| 3 |
|
| 4 |
def get_base64_images():
|
| 5 |
images = {}
|
| 6 |
static_dir = 'static/images'
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
import os
|
| 3 |
|
| 4 |
def get_base64_images():
|
| 5 |
images = {}
|
| 6 |
static_dir = 'static/images'
|
| 7 |
|
| 8 |
+
try:
|
| 9 |
+
# Create static/images directory if it doesn't exist
|
| 10 |
+
os.makedirs(static_dir, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
print(f"Looking for images in: {static_dir}")
|
| 13 |
+
|
| 14 |
+
# Load all image files in the directory
|
| 15 |
+
if os.path.exists(static_dir):
|
| 16 |
+
for image_file in os.listdir(static_dir):
|
| 17 |
+
if image_file.lower().endswith(('.png', '.jpg', '.jpeg')):
|
| 18 |
+
try:
|
| 19 |
+
with open(os.path.join(static_dir, image_file), 'rb') as f:
|
| 20 |
+
# Get filename without extension as the key
|
| 21 |
+
name = os.path.splitext(image_file)[0].lower()
|
| 22 |
+
images[name] = base64.b64encode(f.read()).decode()
|
| 23 |
+
print(f"Loaded: {image_file}")
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Error loading {image_file}: {e}")
|
| 26 |
+
else:
|
| 27 |
+
print(f"Warning: Directory not found: {static_dir}")
|
| 28 |
+
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error in get_base64_images: {e}")
|
| 31 |
+
print(f"Current working directory: {os.getcwd()}")
|
| 32 |
+
|
| 33 |
+
return images
|