Skip to content

Commit

Permalink
fix: sticker resolution (in 0.0.6 version)
Browse files Browse the repository at this point in the history
  • Loading branch information
krypton-byte committed Feb 5, 2024
1 parent 365f8c8 commit e7015c5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
29 changes: 17 additions & 12 deletions neonize/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from google.protobuf.internal.containers import RepeatedCompositeFieldContainer
from linkpreview import link_preview

from .utils.calc import AspectRatioMethod
from .utils.calc import AspectRatioMethod, auto_sticker


from ._binder import gocode, func_string, func_callback_bytes, func
Expand Down Expand Up @@ -587,7 +587,6 @@ def send_sticker(
self,
to: JID,
file: typing.Union[str, bytes],
animated: bool = True,
quoted: Optional[neonize_proto.Message] = None,
name: str = "",
packname: str = "",
Expand All @@ -599,8 +598,6 @@ def send_sticker(
:type to: JID
:param file: The file path of the sticker or the sticker data in bytes.
:type file: typing.Union[str, bytes]
:param animated: Whether the sticker is animated or not, defaults to True.
:type animated: bool, optional
:param quoted: The quoted message, if any, defaults to None.
:type quoted: Optional[neonize_proto.Message], optional
:param name: The name of the sticker, defaults to "".
Expand All @@ -610,15 +607,24 @@ def send_sticker(
:return: The response from the send message function.
:rtype: SendResponse
"""
with FFmpeg(file) as ffmpeg:
sticker = ffmpeg.cv_to_webp(animated)
thumbnail = ffmpeg.extract_thumbnail(ImageFormat.PNG)
sticker = get_bytes_from_name_or_url(file)
animated = False
mime = magic.from_buffer(sticker).split('/')
if mime[0] == "image":
io_save = BytesIO(sticker)
img = Image.open(io_save)
stk = auto_sticker(io_save)
stk.save(io_save, format='webp', exif=add_exif(name, packname), save_all=True, loop=0)
io_save.seek(0)
img.save(
io_save, format="webp", exif=add_exif(name, packname), save_all=True
)
else:
with FFmpeg(sticker) as ffmpeg:
animated = True
sticker = ffmpeg.cv_to_webp()
io_save = BytesIO(sticker)
img = Image.open(io_save)
io_save.seek(0)
img.save(
io_save, format="webp", exif=add_exif(name, packname), save_all=True
)
upload = self.upload(io_save.getvalue())
message = Message(
stickerMessage=StickerMessage(
Expand All @@ -630,7 +636,6 @@ def send_sticker(
mediaKey=upload.MediaKey,
mimetype=magic.from_buffer(io_save.getvalue(), mime=True),
isAnimated=animated,
pngThumbnail=thumbnail,
)
)
if quoted:
Expand Down
9 changes: 5 additions & 4 deletions neonize/utils/calc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from io import BytesIO
from typing import Tuple
from PIL import Image

Expand Down Expand Up @@ -47,7 +48,7 @@ def AspectRatioMethod(
return (res, res)


def sticker_scaler(fn: str):
def sticker_scaler(fn: str | BytesIO | Image.Image):
"""
This function rescales an image to a maximum dimension of 512 pixels while maintaining the aspect ratio.
The function takes the filename of the image as an input and returns the rescaled image.
Expand All @@ -57,12 +58,12 @@ def sticker_scaler(fn: str):
:return: Rescaled image
:rtype: PIL.Image.Image
"""
img = Image.open(fn)
img = fn if isinstance(fn, Image.Image) else Image.open(fn)
width, height = AspectRatioMethod(*img.size, 512)
return img.resize((int(width), int(height)))


def sticker(fn: str):
def auto_sticker(fn: str | BytesIO | Image.Image):
"""
This function creates a new sticker image with a specified size (512 x 512).
The original image is placed at the center of the new sticker.
Expand All @@ -72,7 +73,7 @@ def sticker(fn: str):
:return: The new sticker image with the original image at the center.
:rtype: Image object
"""
img = sticker_scaler(fn)
img = fn if isinstance(fn, Image.Image) else Image.open(fn)
new_layer = Image.new("RGBA", (512, 512), color=(0, 0, 0, 0))
new_layer.paste(img, (256 - (int(img.width / 2)), 256 - (int(img.height / 2))))
return new_layer
4 changes: 2 additions & 2 deletions neonize/utils/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ def cv_to_webp(self, animated: bool = True) -> bytes:
"libwebp",
"-vf",
(
f"scale='if(gt(iw,ih),320,-1)':'if(gt(iw,ih),-1,320)',fps=15, "
f"pad=320:320:-1:-1:[email protected], split [a][b]; [a] "
f"scale='if(gt(iw,ih),512,-1)':'if(gt(iw,ih),-1,512)',fps=15, "
f"pad=512:512:-1:-1:[email protected], split [a][b]; [a] "
f"palettegen=reserve_transparent=on:transparency_color=ffffff [p]; [b][p] paletteuse"
),
temp,
Expand Down

0 comments on commit e7015c5

Please sign in to comment.