stickertopic 支持获取原图 (#62)

Co-authored-by: TNTcraftHIM <TNTcraftHIM@users.noreply.github.com>
This commit is contained in:
AAA 2020-10-11 21:35:57 +08:00 committed by GitHub
parent f281cc98b2
commit 6a31b7dde1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 32 deletions

View File

@ -322,10 +322,10 @@
}, },
{ {
"name": "stickertopic", "name": "stickertopic",
"version": "1.0", "version": "1.1",
"section": "chat", "section": "chat",
"maintainer": "TNTcraftHIM", "maintainer": "TNTcraftHIM",
"size": "3.6 kb", "size": "3.5 kb",
"supported": true, "supported": true,
"des-short": "贴纸转图片", "des-short": "贴纸转图片",
"des": "把别人发的贴纸转换成图片只支持静态。命令pic。" "des": "把别人发的贴纸转换成图片只支持静态。命令pic。"

View File

@ -11,22 +11,37 @@ from random import random
@listener(outgoing=True, command="pic", @listener(outgoing=True, command="pic",
description="将你回复的静态贴纸转换为图片") description="将你回复的静态贴纸转换为图片", parameters="<y/n>是否发送原图默认为n")
async def stickertopic(context): async def stickertopic(context):
try:
if len(context.parameter) >= 1:
if context.parameter[0][0].lower()=="y":
as_file=True
elif context.parameter[0][0].lower()=="n":
as_file=False
elif not context.parameter[0]:
as_file=False
else:
raise IndexError
else:
as_file=False
except:
await context.edit("出错了呜呜呜 ~ 无效的参数。")
return
user = await bot.get_me() user = await bot.get_me()
if not user.username: if not user.username:
user.username = user.first_name user.username = user.first_name
message = await context.get_reply_message() message = await context.get_reply_message()
custom_emoji = False custom_emoji = False
animated = False animated = False
await context.edit("开始转换...\n███30%") await context.edit("开始转换...\n0%")
if message and message.media: if message and message.media:
if isinstance(message.media, MessageMediaPhoto): if isinstance(message.media, MessageMediaPhoto):
photo = BytesIO() photo = BytesIO()
photo = await bot.download_media(message.photo, photo) photo = await bot.download_media(message.photo, photo)
elif "image" in message.media.document.mime_type.split('/'): elif "image" in message.media.document.mime_type.split('/'):
photo = BytesIO() photo = BytesIO()
await context.edit("正在转换...\n██████60%") await context.edit("正在转换...\n████40%")
await bot.download_file(message.media.document, photo) await bot.download_file(message.media.document, photo)
if (DocumentAttributeFilename(file_name='sticker.webp') in if (DocumentAttributeFilename(file_name='sticker.webp') in
message.media.document.attributes): message.media.document.attributes):
@ -62,9 +77,9 @@ async def stickertopic(context):
return return
if not animated: if not animated:
await context.edit("正在转换...\n█████████90%") await context.edit("正在转换...\n███████70%")
image = await resize_image(photo) image = Image.open(photo)
filename = "sticker"+str(random())+".png" filename = "sticker"+str(random())[2:]+".png"
image.save(filename, "PNG") image.save(filename, "PNG")
else: else:
await context.edit("出错了呜呜呜 ~ 目标不是**静态**贴纸 。") await context.edit("出错了呜呜呜 ~ 目标不是**静态**贴纸 。")
@ -72,7 +87,7 @@ async def stickertopic(context):
await context.delete() await context.delete()
return return
await context.edit("正在上传...\n██████████99%") await context.edit("正在上传...\n██████████99%")
await bot.send_file(context.chat_id,filename) await bot.send_file(context.chat_id,filename, force_document=as_file)
try: try:
await context.delete() await context.delete()
except: except:
@ -85,26 +100,3 @@ async def stickertopic(context):
remove("AnimatedSticker.tgs") remove("AnimatedSticker.tgs")
except: except:
pass pass
async def resize_image(photo):
image = Image.open(photo)
maxsize = (512, 512)
if (image.width and image.height) < 512:
size1 = image.width
size2 = image.height
if image.width > image.height:
scale = 512 / size1
size1new = 512
size2new = size2 * scale
else:
scale = 512 / size2
size1new = size1 * scale
size2new = 512
size1new = floor(size1new)
size2new = floor(size2new)
size_new = (size1new, size2new)
image = image.resize(size_new)
else:
image.thumbnail(maxsize)
return image