import io import base64 import json from PIL import Image from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel import uvicorn import os for i in os.listdir('control_imgs'): if i.endswith('.png'): img_path = f'control_imgs/{i}' img = Image.open(img_path) w, h = img.width, img.height if w != 1024 or h != 1024: if w > h: new_w = 1024 new_h = max(1, int(round(h * 1024 / w))) else: new_h = 1024 new_w = max(1, int(round(w * 1024 / h))) img = img.resize((new_w, new_h), Image.Resampling.LANCZOS) new_img = Image.new('RGBA', (1024, 1024), (0, 0, 0, 0)) new_img.paste(img, ((1024 - new_w) // 2, (1024 - new_h) // 2)) img = new_img img.save(img_path)