Spaces:
Running
Running
import gradio as gr | |
from PIL import Image | |
from io import BytesIO | |
import subprocess | |
import tempfile | |
# Assuming your functions are in a module named 'image_utils' | |
from tiles_to_gif import tile_and_convert | |
def process_image(image, tile_type, lossy=80, colors=200): | |
"""Process the image, apply tiling, create GIF, and compress it.""" | |
# Tile the image | |
gif_buffer = tile_and_convert(image, tile_type) | |
# Compress using Gifsicle | |
command = ["gifsicle", "--optimize", f"--lossy={lossy}", f"--colors={colors}", "-"] | |
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
compressed_data, _ = process.communicate(input=gif_buffer.getvalue()) | |
# Create a temporary file | |
with tempfile.NamedTemporaryFile(suffix=".gif", delete=False) as temp_file: | |
temp_file.write(compressed_data) | |
gif_path = temp_file.name # Get the file path | |
# Return the path of the temporary GIF file | |
return gif_path | |
docstring = """ | |
Upload a 1x1, 1x2, 2x1, or 2x2 downloaded tile from ideogram. Choose the same tiling type as what you | |
generated with to create a gif. The program is a bit slow, because gifs aren't great. It is optimised to create | |
small previews for the ideogram discord (so less than 25MB), however, it might still be that a very detailed and colourful | |
image exceeds this file size. Possible sources of error include uploading a file which is too large (e.g. 4x4 tile) | |
or forgetting to choose the tiling type parameter from the drop down menu. | |
""" | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=process_image, | |
allow_flagging="never", | |
description=docstring, | |
inputs=[ | |
gr.Image(type="pil"), | |
gr.Dropdown(["row", "column", "grid", "vertical_brick", "horizontal_brick"], label="Tiling Type") | |
], | |
outputs=gr.File(label="Download Compressed GIF"), | |
title="Ideogram Tile GIF creator" | |
) | |
iface.launch() |