Supan23 commited on
Commit
b27fa09
·
verified ·
1 Parent(s): ffe3b03

Create image_processing.py

Browse files
Files changed (1) hide show
  1. image_processing.py +25 -0
image_processing.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ import base64
4
+ import uuid
5
+ from PIL import Image
6
+
7
+ # Helper functions for image processing
8
+
9
+ def encode_image(image_path: str) -> str:
10
+ """Convert an image file to base64 string."""
11
+ with open(image_path, "rb") as image_file:
12
+ return base64.b64encode(image_file.read()).decode("utf-8")
13
+
14
+ def decode_image(base64_string: str) -> Image.Image:
15
+ """Convert a base64 string to a PIL Image."""
16
+ image_data = base64.b64decode(base64_string)
17
+ return Image.open(io.BytesIO(image_data))
18
+
19
+ def save_image(image: Image.Image, directory: str = "image_outputs") -> str:
20
+ """Save a PIL Image to disk and return the path."""
21
+ os.makedirs(directory, exist_ok=True)
22
+ image_id = str(uuid.uuid4())
23
+ image_path = os.path.join(directory, f"{image_id}.png")
24
+ image.save(image_path)
25
+ return image_path