rDrama/files/helpers/images.py

38 lines
1.0 KiB
Python
Raw Normal View History

2021-07-21 01:12:26 +00:00
import requests
2021-10-01 06:37:50 +00:00
from os import environ
2021-09-15 00:46:08 +00:00
from PIL import Image as IImage, ImageSequence
2021-08-04 15:35:10 +00:00
from files.classes.images import *
2021-09-15 09:15:56 +00:00
from webptools import gifwebp
2021-07-21 01:12:26 +00:00
2021-09-30 02:22:42 +00:00
CATBOX_KEY = environ.get("CATBOX_KEY", "").strip()
2021-09-02 14:06:28 +00:00
2021-09-13 16:35:26 +00:00
def upload_ibb(file=None, resize=False):
if file: file.save("image.webp")
2021-09-15 09:29:07 +00:00
i = IImage.open("image.webp")
2021-09-13 17:01:55 +00:00
if resize:
size = 100, 100
frames = ImageSequence.Iterator(i)
2021-09-13 16:56:22 +00:00
2021-09-13 17:01:55 +00:00
def thumbnails(frames):
for frame in frames:
2021-09-13 16:58:15 +00:00
thumbnail = frame.copy()
2021-09-15 00:46:25 +00:00
thumbnail.thumbnail(size)
2021-09-13 16:58:15 +00:00
yield thumbnail
2021-09-13 17:01:55 +00:00
frames = thumbnails(frames)
2021-09-13 16:56:22 +00:00
2021-09-13 17:01:55 +00:00
om = next(frames)
om.info = i.info
2021-09-15 16:57:43 +00:00
om.save("image.webp", save_all=True, append_images=list(frames), loop=0)
2021-09-23 16:47:48 +00:00
elif i.format.lower() != "webp":
2021-09-16 10:09:26 +00:00
if i.format.lower() == "gif": gifwebp(input_image="image.webp", output_image="image.webp", option="-q 80")
2021-09-15 16:57:43 +00:00
else: i.save("image.webp")
2021-09-15 00:42:12 +00:00
2021-09-15 09:17:07 +00:00
with open("image.webp", 'rb') as f:
2021-10-02 00:36:20 +00:00
req = requests.post('https://catbox.moe/user/api.php', data={'userhash':CATBOX_KEY, 'reqtype':'fileupload'}, files={'fileToUpload':f}).text
2021-09-13 16:35:26 +00:00
2021-10-02 00:36:20 +00:00
if req.startswith('https://'): return req