Spaces:
Running
Running
Luong Huu Thanh
commited on
Commit
·
a6b2731
1
Parent(s):
11bec25
Create image_processing.py
Browse files- image_processing.py +26 -0
image_processing.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
def encode_image(image_path: str) -> str:
|
9 |
+
"""Convert an image file to base64 string."""
|
10 |
+
with open(image_path, "rb") as image_file:
|
11 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
12 |
+
|
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 |
+
|
20 |
+
def save_image(image: Image.Image, directory: str = "image_outputs") -> str:
|
21 |
+
"""Save a PIL Image to disk and return the path."""
|
22 |
+
os.makedirs(directory, exist_ok=True)
|
23 |
+
image_id = str(uuid.uuid4())
|
24 |
+
image_path = os.path.join(directory, f"{image_id}.png")
|
25 |
+
image.save(image_path)
|
26 |
+
return image_path
|