images2collage / app.py
Weaita's picture
Update app.py
b8cb9cc verified
raw
history blame
2.26 kB
import gradio as gr
import numpy as np
import zipfile
from PIL import Image
import tempfile
rows = 3
cols = 3
def create_collage(images, image_size):
image_size = int(image_size)
#Determinar el n煤mero de collages
num_collages = len(images) // (rows*cols)
with zipfile.ZipFile("collages.zip", 'w') as zip:
for i in range(num_collages):
#Crear una nueva imagen en blanco
result = Image.new('RGB', (cols*image_size, rows*image_size), (255, 255, 255))
for j in range(rows*cols):
#Abrir imagen
img = Image.open(images[i*(rows*cols)+j])
#Reimage_sizeensionar imagen
img = img.resize((image_size, image_size))
#Determinar la posici贸n de la imagen en el collage
pos_x = (j % cols) * image_size
pos_y = (j // cols) * image_size
#Pegar la imagen en el collage
result.paste(img, (pos_x, pos_y))
#Guardar el collage
with tempfile.NamedTemporaryFile(suffix='.png') as temp:
result.save(temp.name)
zip.write(temp.name)
temp.flush()
temp.close()
last_image = Image.new('RGB', (cols*image_size, rows*image_size), (255, 255, 255))
for j in range(len(images) % (rows*cols)):
#Abrir imagen
img = Image.open(images[num_collages*(rows*cols)+j])
#Reimage_sizeensionar imagen
img = img.resize((image_size, image_size))
#Determinar la posici贸n de la imagen en el collage
pos_x = (j % cols) * image_size
pos_y = (j // cols) * image_size
#Pegar la imagen en el collage
last_image.paste(img, (pos_x, pos_y))
#Guardar el collage
with tempfile.NamedTemporaryFile(suffix='.png') as temp:
last_image.save(temp.name)
zip.write(temp.name)
temp.flush()
temp.close()
return "collages.zip"
gr.Interface( fn=create_collage,
inputs=["files", gr.Radio(["512", "768"])],
outputs="files",
title="Convert images to collage"
).launch();