File size: 924 Bytes
5bbe62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)