from math import cos, radians, sin from flask import Flask, render_template, request, send_file, jsonify from PIL import Image, ImageDraw, ImageOps import gradio as gr import os import io import subprocess import cairosvg app = Flask(__name__) UPLOAD_FOLDER = 'uploads' os.makedirs(UPLOAD_FOLDER, exist_ok=True) def preprocess_image(image): # Ensure the image is square by padding it size = max(image.size) new_image = Image.new("RGBA", (size, size), (255, 255, 255, 0)) new_image.paste(image, ((size - image.width) // 2, (size - image.height) // 2)) return new_image def convert_webp_to_jpeg(image_data): try: image = Image.open(io.BytesIO(image_data)) if image.format == 'WEBP': output = io.BytesIO() image.convert('RGB').save(output, format='JPEG') return output.getvalue() return image_data except Exception as e: raise ValueError(f"Error converting WEBP to JPEG: {e}") def convert_svg_to_png(image_data): try: png_data = cairosvg.svg2png(bytestring=image_data) return png_data except Exception as e: raise ValueError(f"Error converting SVG to PNG: {e}") def create_gif(images): frames = [] duration = 100 # Duration for each frame in milliseconds total_frames = 18 # Total number of frames try: img1_data = images[0].read() img2_data = images[1].read() # Convert WebP images to JPEG if necessary img1_data = convert_webp_to_jpeg(img1_data) img2_data = convert_webp_to_jpeg(img2_data) # Convert SVG images to PNG if necessary if img1_data.strip().startswith(b'