collage2images / app.py
Weaita's picture
Update app.py
f953e00
raw
history blame
986 Bytes
import gradio as gr
import numpy as np
import zipfile
from PIL import Image
import tempfile
def split_image(image):
im = Image.fromarray(np.uint8(image))
# Obtiene las dimensiones de la imagen
width, height = im.size
# Divide la imagen en 9 imágenes más pequeñas
for i in range(3):
for j in range(3):
box = (j*width/3, i*height/3, (j+1)*width/3, (i+1)*height/3)
region = im.crop(box)
region = region.convert("RGB")
region = region.resize((512, 512), Image.ANTIALIAS)
with tempfile.NamedTemporaryFile(suffix='.jpg') as temp:
region.save(temp.name)
with zipfile.ZipFile("image.zip", 'w') as zip:
zip.write(temp.name)
return "image.zip"
gr.Interface(fn=split_image,
inputs="image",
outputs="file",
title="Convert collage to images",
examples = ["example.jpg"]
).launch();