Skip to content

Commit

Permalink
fix: sticker_refactor convert image failed
Browse files Browse the repository at this point in the history
  • Loading branch information
guimc233 committed Jul 23, 2023
1 parent ca3ce0f commit 3d47ae2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion list.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"list": [
{
"name": "sticker_refactor",
"version": "1.09",
"version": "1.099",
"section": "chat",
"maintainer": "Guimc",
"size": "9.22kb",
Expand Down
27 changes: 18 additions & 9 deletions sticker_refactor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def __init__(self, msg: str = ""):
super().__init__(msg)


def get_tempfile() -> str:
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as f:
def get_tempfile(suffix: str = ".png") -> str:
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
return f.name


Expand Down Expand Up @@ -152,18 +152,27 @@ async def download_photo(msg: Message) -> str:
raise GeneralError("下载媒体失败.") from e


async def download_sticker(msg: Message) -> str:
try:
filename = get_tempfile(".webp")
await bot.download_media(msg, filename)
return filename
except Exception as e:
raise GeneralError("下载媒体失败.") from e


def convert_image(imgfile: str) -> str:
try:
img = Image.open(imgfile)
width, height = img.size

if (width >= 512 or height >= 512) or (width <= 512 and height <= 512):
if width >= height:
scaling = 512 / width
else:
scaling = 512 / height
scaling = height / width

img = img.resize((int(width * scaling), int(height * scaling)), IMAGE_IMPROVE)
if scaling <= 1:
img = img.resize((512, int(512 * scaling)), IMAGE_IMPROVE)
else:
img = img.resize((int(512 / scaling), 512), IMAGE_IMPROVE)
img.save(imgfile + "_patched.png")

return imgfile + "_patched.png"
Expand Down Expand Up @@ -248,15 +257,15 @@ async def sticker_refactor(msg: Message):
if msg.reply_to_message:
_emoji = get_emoji()

if msg.reply_to_message.sticker:
if msg.reply_to_message.sticker and msg.reply_to_message.sticker.emoji is not None:
_emoji = msg.reply_to_message.sticker.emoji

if len(msg.parameter) == 1 and is_emoji(msg.arguments):
_emoji = msg.arguments

# check target type
if msg.reply_to_message.sticker:
await add_to_stickers(msg.reply_to_message, _emoji)
await file2sticker(await download_sticker(msg.reply_to_message), _emoji)
elif msg.reply_to_message.photo:
await file2sticker(await download_photo(msg.reply_to_message), _emoji)
elif msg.reply_to_message.document:
Expand Down

0 comments on commit 3d47ae2

Please sign in to comment.