From c9996256808aa95796fb0b13ba78da7f5d160125 Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 18 Jul 2023 22:29:48 -0400 Subject: [PATCH] changing from pyvips to PIL to write the gif --- responses.py | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/responses.py b/responses.py index 20b2f51..f601b94 100644 --- a/responses.py +++ b/responses.py @@ -2,7 +2,10 @@ import random import seventv import requests from PIL import Image -from io import BytesIO +import imageio +import requests +import os +import io import pyvips @@ -25,6 +28,8 @@ def get_response(message: str): def helpText(): return '`This is a help message that you can modify.`' +''' +keeping old code just in case pyvips needs to be used def addGif(url): webpUrl = seventv.get_webp_url(url) @@ -34,9 +39,45 @@ def addGif(url): return ("Invalid URL") image = pyvips.Image.new_from_buffer(webp_data, '') - gif_data = image.write_to_buffer('.gif') + gif_data = image.write_to_buffer('.gif[loop=0]') - gif_name = seventv.get_emote_name(url) + gif_name = s + + with open(gif_name, 'wb') as f: + f.write(gif_data) return gif_data, gif_name +''' + + +def addGif(url): + webpUrl = seventv.get_webp_url(url) + try: + webp_data = requests.get(webpUrl).content + except requests.exceptions.RequestException: + return ("Invalid URL") + + # Extract the name from the url and replace .webp extension with .gif + gif_name = seventv.get_emote_name(url) + + # Open webp image with PIL + image = Image.open(io.BytesIO(webp_data)) + + # If image is an animated webp, PIL will open it as a sequence of frames + frames = [] + try: + while True: + frames.append(image.copy()) + image.seek(len(frames)) # Skip to next frame + except EOFError: + pass # We have read all the frames from the image now + + # Create a byte buffer and save the gif data into it + gif_data_buffer = io.BytesIO() + imageio.mimwrite(gif_data_buffer, frames, 'GIF', loop=0) + + # Get the gif data as bytes + gif_data = gif_data_buffer.getvalue() + + return gif_data, gif_name \ No newline at end of file