gkc55 commited on
Commit
6248155
·
1 Parent(s): e79bab1

Fix image_utils.py to handle all images in single directory

Browse files
Files changed (1) hide show
  1. 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
- # Logo and banner
9
- with open(os.path.join(static_dir, 'logo.png'), 'rb') as f:
10
- images['logo'] = base64.b64encode(f.read()).decode()
11
-
12
- with open(os.path.join(static_dir, 'banner.png'), 'rb') as f:
13
- images['banner'] = base64.b64encode(f.read()).decode()
14
-
15
- # Crop images
16
- crops_dir = os.path.join(static_dir, 'crops')
17
- for image_file in os.listdir(crops_dir):
18
- if image_file.endswith(('.jpg', '.png')):
19
- with open(os.path.join(crops_dir, image_file), 'rb') as f:
20
- images[image_file.split('.')[0]] = base64.b64encode(f.read()).decode()
21
-
22
- return images
 
 
 
 
 
 
 
 
 
 
 
 
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