changing from pyvips to PIL to write the gif

This commit is contained in:
Juan 2023-07-18 22:29:48 -04:00
parent a2b4e5127d
commit c999625680

View file

@ -2,7 +2,10 @@ import random
import seventv import seventv
import requests import requests
from PIL import Image from PIL import Image
from io import BytesIO import imageio
import requests
import os
import io
import pyvips import pyvips
@ -25,6 +28,8 @@ def get_response(message: str):
def helpText(): def helpText():
return '`This is a help message that you can modify.`' return '`This is a help message that you can modify.`'
'''
keeping old code just in case pyvips needs to be used
def addGif(url): def addGif(url):
webpUrl = seventv.get_webp_url(url) webpUrl = seventv.get_webp_url(url)
@ -34,9 +39,45 @@ def addGif(url):
return ("Invalid URL") return ("Invalid URL")
image = pyvips.Image.new_from_buffer(webp_data, '') 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 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