Fix Animated Sticker / TGS Sticker Transparency Issues#139
Open
Ovler-Young wants to merge 13 commits intoehForwarderBot:masterfrom
Open
Fix Animated Sticker / TGS Sticker Transparency Issues#139Ovler-Young wants to merge 13 commits intoehForwarderBot:masterfrom
Ovler-Young wants to merge 13 commits intoehForwarderBot:masterfrom
Conversation
blueset
requested changes
Mar 19, 2024
Member
blueset
left a comment
There was a problem hiding this comment.
Thank you for the contribution!
| # (libcairo) | ||
| from lottie.exporters.cairo import export_png | ||
| from lottie.exporters.gif import _png_gif_prepare | ||
| # from lottie.exporters.gif import _png_gif_prepare # The code here have some problem, so I copy the function abo ve |
Member
There was a problem hiding this comment.
Since the forked code is already above, this line can be removed.
| frames.append(_png_gif_prepare(Image.open(file))) | ||
|
|
||
| duration = 1000 / animation.frame_rate * (1 + skip_frames) / 2 | ||
| duration = 1000 / animation.frame_rate * (1 + skip_frames) # why /2 before? |
Member
There was a problem hiding this comment.
The / 2 was brought over as a part of the original function. I’m not quite sure what it stands for though.
Comment on lines
+279
to
+282
| split = ( | ||
| stream | ||
| .split() | ||
| ) |
Member
There was a problem hiding this comment.
Can be simplifed.
Suggested change
| split = ( | |
| stream | |
| .split() | |
| ) | |
| split = stream.split() |
Comment on lines
+283
to
+296
| stream_paletteuse = ( | ||
| ffmpeg | ||
| .filter( | ||
| [ | ||
| split[0], | ||
| split[1] | ||
| .filter( | ||
| filter_name='palettegen', | ||
| reserve_transparent='on', | ||
| ) | ||
| ], | ||
| filter_name='paletteuse', | ||
| ) | ||
| ) |
Member
There was a problem hiding this comment.
Can be simplified:
Suggested change
| stream_paletteuse = ( | |
| ffmpeg | |
| .filter( | |
| [ | |
| split[0], | |
| split[1] | |
| .filter( | |
| filter_name='palettegen', | |
| reserve_transparent='on', | |
| ) | |
| ], | |
| filter_name='paletteuse', | |
| ) | |
| ) | |
| stream_paletteuse = ffmpeg.filter([ | |
| split[0], | |
| split[1].filter( | |
| filter_name='palettegen', | |
| reserve_transparent='on', | |
| ) | |
| ], filter_name='paletteuse')) |
Comment on lines
+329
to
+331
| # 检查视频编码类型是否为VP9 | ||
| if metadata['streams'][0]['codec_name'] == 'vp9': | ||
| stream = ffmpeg.input(file.name, vcodec='libvpx-vp9') # 只有这个能保持透明背景 |
Member
There was a problem hiding this comment.
Please keep code comments in English.
Comment on lines
+332
to
+349
| split = ( | ||
| stream | ||
| .split() | ||
| ) | ||
| stream_paletteuse = ( | ||
| ffmpeg | ||
| .filter( | ||
| [ | ||
| split[0], | ||
| split[1] | ||
| .filter( | ||
| filter_name='palettegen', | ||
| reserve_transparent='on', | ||
| ) | ||
| ], | ||
| filter_name='paletteuse', | ||
| ) | ||
| ) |
| ) | ||
| stream_paletteuse.output(gif_file.name).overwrite_output().run() | ||
| new_file_size = os.path.getsize(gif_file.name) | ||
| print(f"file_size: {new_file_size/1024}KB") |
Member
There was a problem hiding this comment.
Use logging for debug logs. Avoid using print directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
During the process of investigating the GIF size limits, I also discovered that even if a sticker has transparency, this transparency is lost when the sticker is sent to WeChat:

To address this transparency issue, I found that decoding the WEBM files from Telegram correctly and using palettegen with the reserve_transparent flag, then encoding to GIF can retain transparency in a way that WeChat recognizes.
This pull request also tried resolves the transparency loss issue when sending tgs stickers from Telegram to WeChat via EFB, but there might still be an issue with the first frame of some TGS stickers having a black background.
在研究 GIF 大小限制的过程中, 我还发现即使原始贴图有透明背景, 发送到微信后也会丢失透明度, 变成不透明的黑色背景:

为了解决这个透明度丢失的问题, 我发现正确解码 Telegram 的 WEBM 文件, 并在生成调色板时使用 reserve_transparent 标志, 然后编码成 GIF, 就能以微信可识别的方式保留透明背景。
该 Pull Request 还尝试解决了从 Telegram 通过 EFB 发送 tgs 格式的贴图到微信时透明度丢失的问题,但某些 TGS 贴图的第一帧可能仍然会有黑色背景。