maunium / stickerpicker

A fast and simple Matrix sticker picker widget
GNU Affero General Public License v3.0
325 stars 612 forks source link

Telegram sticker packs with animated stickers fail to import #79

Open Azel04 opened 2 months ago

Azel04 commented 2 months ago

Attempting to import any Telegram Sticker Pack with an animated sticker results in this error

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/REDACTED/stickerpicker/.venv/bin/sticker-import", line 8, in sys.exit(cmd()) ^^^^^ File "/home/REDACTED/stickerpicker/.venv/lib/python3.12/site-packages/sticker/stickerimport.py", line 161, in cmd asyncio.get_event_loop().run_until_complete(main(parser.parse_args())) File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/home/REDACTED/stickerpicker/.venv/lib/python3.12/site-packages/sticker/stickerimport.py", line 153, in main await reupload_pack(client, pack, args.output_dir) File "/home/REDACTED/stickerpicker/.venv/lib/python3.12/site-packages/sticker/stickerimport.py", line 84, in reupload_pack reuploaded_documents[document.id] = await reupload_document(client, document) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/REDACTED/stickerpicker/.venv/lib/python3.12/site-packages/sticker/stickerimport.py", line 36, in reupload_document data, width, height = util.convert_image(data) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/REDACTED/stickerpicker/.venv/lib/python3.12/site-packages/sticker/lib/util.py", line 28, in convert_image image: Image.Image = Image.open(BytesIO(data)).convert("RGBA") ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/REDACTED/stickerpicker/.venv/lib/python3.12/site-packages/PIL/Image.py", line 3498, in open raise UnidentifiedImageError(msg) PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x74c06caf7600>

The sticker pack i tried is this one: https://t.me/addstickers/precure2

robertototaro commented 1 month ago

I can reproduce this problem with other Telegram sticker packs, for example: https://t.me/addstickers/Hedgehoge https://t.me/addstickers/NickWallowPig https://t.me/addstickers/MooingCow

Azel04 commented 1 month ago

After quickly looking into the code, i think there may be a way to fix this.

Animated Telegram Stickers use the WEBM format which apparently isn't compatible with the image conversion library used here.

Maybe we can convert WEBM to another format like GIF with FFMpeg? Or maybe we can change the image conversion library to another one that supports WEBM.

3ttercap commented 1 week ago

After quickly looking into the code, i think there may be a way to fix this.

Animated Telegram Stickers use the WEBM format which apparently isn't compatible with the image conversion library used here.

Maybe we can convert WEBM to another format like GIF with FFMpeg? Or maybe we can change the image conversion library to another one that supports WEBM.

yes, I used pylottie, playwright, Pillow at https://pypi.org/project/pyrlottie/ (https://github.com/FHPythonUtils/PyLottie/blob/master/README.md) which help converting the .tgs sticker file to .gif file, which could be used here right?

a quick demo code:

import os from pylottie import convertMultLottie2ALL

def convert_single_tgs_to_gif(tgs_file, output_file):

Convert the single TGS file to the specified output format, multiple files supported.

convertMultLottie2ALL([tgs_file], [output_file])

if name == "main":

Define the input and output file paths

input_file = "animation.tgs"  # Example TGS file
output_file = "animation"   # Corresponding output file without extension

# Check if the input file exists
if not os.path.exists(input_file):
    print(f"Input file '{input_file}' does not exist.")
else:
    # Convert the TGS file to GIF
    convert_single_tgs_to_gif(input_file, output_file)