Spaces:
Running
Running
File size: 2,256 Bytes
b8cb9cc 640cf7c 2213b12 640cf7c 4ad4ba4 640cf7c 7b07b3e 640cf7c b71309d 4ad4ba4 |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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(); |