Style_Sync / utils.py
heramb04's picture
Upload 4 files
3369069 verified
import os
import random
from datetime import datetime
from PIL import Image
import matplotlib.pyplot as plt
# πŸ“¦ Resize + convert image
def resize_image(image, size=(512, 512)):
return image.convert("RGB").resize(size)
# πŸ“‚ Save output with timestamp
def save_output(image, save_dir="output", prefix="stylized"):
os.makedirs(save_dir, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{prefix}_{timestamp}.jpg"
path = os.path.join(save_dir, filename)
image.save(path)
return path
# πŸ“œ Log content + style pairing
def log_run(content_name, style_name, log_path="log.txt"):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
line = f"{timestamp} | Content: {content_name} + Style: {style_name} = Stylized\n"
with open(log_path, "a") as f:
f.write(line)
# πŸ–ΌοΈ Triptych visualizer: A + B = C
def show_triptych(content_img, style_img, output_img):
fig, axs = plt.subplots(1, 3, figsize=(15, 5))
axs[0].imshow(content_img); axs[0].set_title("A: Content"); axs[0].axis('off')
axs[1].imshow(style_img); axs[1].set_title("B: Style"); axs[1].axis('off')
axs[2].imshow(output_img); axs[2].set_title("C: Output"); axs[2].axis('off')
plt.suptitle("A + B = C", fontsize=20)
plt.show()