diff --git a/neonize/client.py b/neonize/client.py index fa430e4..70f6cdd 100644 --- a/neonize/client.py +++ b/neonize/client.py @@ -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 @@ -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 = "", @@ -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 "". @@ -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( @@ -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: diff --git a/neonize/utils/calc.py b/neonize/utils/calc.py index 97c3d3a..a1f3f84 100644 --- a/neonize/utils/calc.py +++ b/neonize/utils/calc.py @@ -1,3 +1,4 @@ +from io import BytesIO from typing import Tuple from PIL import Image @@ -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. @@ -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. @@ -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 diff --git a/neonize/utils/ffmpeg.py b/neonize/utils/ffmpeg.py index 8694895..4643ab4 100644 --- a/neonize/utils/ffmpeg.py +++ b/neonize/utils/ffmpeg.py @@ -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:color=white@0.0, 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:color=white@0.0, split [a][b]; [a] " f"palettegen=reserve_transparent=on:transparency_color=ffffff [p]; [b][p] paletteuse" ), temp,