Spaces:
Running
on
Zero
Running
on
Zero
Update utils.py
Browse files
utils.py
CHANGED
@@ -5,7 +5,7 @@ import numpy as np
|
|
5 |
import json
|
6 |
import torch
|
7 |
import uuid
|
8 |
-
from PIL import Image
|
9 |
from datetime import datetime
|
10 |
from dataclasses import dataclass
|
11 |
from typing import Callable, Dict, Optional, Tuple
|
@@ -162,21 +162,26 @@ def preprocess_image_dimensions(width, height):
|
|
162 |
def save_image(image, metadata, output_dir, is_colab):
|
163 |
if is_colab:
|
164 |
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
|
165 |
-
filename = f"image_{current_time}.
|
166 |
else:
|
167 |
-
filename = str(uuid.uuid4()) + ".
|
168 |
os.makedirs(output_dir, exist_ok=True)
|
169 |
filepath = os.path.join(output_dir, filename)
|
170 |
-
metadata_str = json.dumps(metadata)
|
171 |
-
info = PngImagePlugin.PngInfo()
|
172 |
-
info.add_text("metadata", metadata_str)
|
173 |
-
image.save(filepath, "PNG", pnginfo=info)
|
174 |
-
return filepath
|
175 |
|
|
|
|
|
|
|
|
|
|
|
176 |
|
|
|
|
|
|
|
|
|
|
|
177 |
def is_google_colab():
|
178 |
try:
|
179 |
import google.colab
|
180 |
return True
|
181 |
except:
|
182 |
-
return False
|
|
|
5 |
import json
|
6 |
import torch
|
7 |
import uuid
|
8 |
+
from PIL import Image
|
9 |
from datetime import datetime
|
10 |
from dataclasses import dataclass
|
11 |
from typing import Callable, Dict, Optional, Tuple
|
|
|
162 |
def save_image(image, metadata, output_dir, is_colab):
|
163 |
if is_colab:
|
164 |
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
|
165 |
+
filename = f"image_{current_time}.jpg"
|
166 |
else:
|
167 |
+
filename = str(uuid.uuid4()) + ".jpg"
|
168 |
os.makedirs(output_dir, exist_ok=True)
|
169 |
filepath = os.path.join(output_dir, filename)
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
+
# Lưu metadata dưới dạng tệp văn bản đi kèm
|
172 |
+
metadata_str = json.dumps(metadata)
|
173 |
+
metadata_filepath = os.path.join(output_dir, f"{filename}.json")
|
174 |
+
with open(metadata_filepath, 'w') as f:
|
175 |
+
f.write(metadata_str)
|
176 |
|
177 |
+
# Lưu hình ảnh dưới định dạng JPEG
|
178 |
+
image.save(filepath, "JPEG")
|
179 |
+
return filepath
|
180 |
+
|
181 |
+
|
182 |
def is_google_colab():
|
183 |
try:
|
184 |
import google.colab
|
185 |
return True
|
186 |
except:
|
187 |
+
return False
|