Skip to content

Commit bf8e7a0

Browse files
committed
✨ feat(tg_parse_hub): 添加图片格式兼容处理,支持HEIF/HEIC转WEBP以适配Telegram显示
✨ feat(utiles/utile): 实现img2webp函数将HEIF图像转换为WEBP格式的二进制流 ✨ feat(bot): 注册pillow_heif解码器,支持HEIF图片加载 🆕 chore(pyproject): 新增pillow和pillow-heif依赖支持HEIF格式处理
1 parent d99b7dc commit bf8e7a0

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

methods/tg_parse_hub.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,24 @@ def f_text(text: str) -> str:
502502
else:
503503
return text
504504

505+
@staticmethod
506+
async def tg_compatible(img: str | Path) -> BinaryIO | str:
507+
"""将图片转换为Tg兼容的格式"""
508+
509+
ext = Path(img).suffix.lower()
510+
if ext not in [".heif", ".heic"]:
511+
return str(img)
512+
513+
loop = asyncio.get_running_loop()
514+
try:
515+
return await asyncio.wait_for(
516+
loop.run_in_executor(EXC, img2webp, img),
517+
timeout=30,
518+
)
519+
except Exception as e:
520+
logger.exception(e)
521+
return str(img)
522+
505523

506524
class VideoParseResultOperate(ParseResultOperate):
507525
"""视频解析结果操作"""
@@ -576,7 +594,7 @@ async def chat_upload(
576594
)
577595
elif count == 1:
578596
return await msg.reply_photo(
579-
self.download_result.media[0].path,
597+
await self.tg_compatible(self.download_result.media[0].path),
580598
quote=True,
581599
caption=text,
582600
reply_markup=self.button(),
@@ -616,25 +634,6 @@ async def limited_ih(path: str):
616634
f"{self.result.desc}<br><br>" + "".join(results), msg
617635
)
618636

619-
@staticmethod
620-
async def tg_compatible(img: str | Path) -> BinaryIO | str:
621-
"""将图片转换为Tg兼容的格式"""
622-
623-
ext = Path(img).suffix.lower()
624-
if ext not in [".heif", ".heic"]:
625-
return str(img)
626-
627-
loop = asyncio.get_running_loop()
628-
try:
629-
r = await asyncio.wait_for(
630-
loop.run_in_executor(EXC, img2webp, img),
631-
timeout=30,
632-
)
633-
return r
634-
except Exception as e:
635-
logger.exception(e)
636-
return str(img)
637-
638637

639638
class MultimediaParseResultOperate(ParseResultOperate):
640639
"""图片视频混合解析结果操作"""
@@ -661,7 +660,7 @@ async def chat_upload(
661660
"reply_markup": self.button(),
662661
}
663662
if isinstance(m, Image):
664-
return await msg.reply_photo(m.path, **k)
663+
return await msg.reply_photo(await self.tg_compatible(m.path), **k)
665664
elif isinstance(m, Video):
666665
return await msg.reply_video(
667666
m.path,
@@ -682,7 +681,7 @@ async def chat_upload(
682681
ani_msg = []
683682
for i, v in enumerate(self.download_result.media):
684683
if isinstance(v, Image):
685-
media.append(InputMediaPhoto(v.path))
684+
media.append(InputMediaPhoto(await self.tg_compatible(v.path)))
686685
elif isinstance(v, Video):
687686
media.append(
688687
InputMediaVideo(

0 commit comments

Comments
 (0)