diff --git a/optional-requirements.txt b/optional-requirements.txt index 55bfc014..aeaf3994 100644 --- a/optional-requirements.txt +++ b/optional-requirements.txt @@ -1,3 +1,4 @@ cryptg pysocks hachoir3 +pillow diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index 57144add..91ce0279 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -31,6 +31,7 @@ class _CacheType: def _resize_photo_if_needed(file, is_image, width=1280, height=1280): + # https://github.com/telegramdesktop/tdesktop/blob/12905f0dcb9d513378e7db11989455a1b764ef75/Telegram/SourceFiles/boxes/photo_crop_box.cpp#L254 if (not is_image or PIL is None or (isinstance(file, io.IOBase) and not file.seekable())): @@ -164,6 +165,11 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): If the ``hachoir3`` package (``hachoir`` module) is installed, it will be used to determine metadata from audio and video files. + If the `pillow` package is installed and you are sending a photo, + it will be resized to fit within the maximum dimensions allowed + by Telegram to avoid ``errors.PhotoInvalidDimensionsError``. This + cannot be done if you are sending :tl:`InputFile`, however. + Returns: The `telethon.tl.custom.message.Message` (or messages) containing the sent file, or messages if a list of them was passed. diff --git a/telethon_generator/data/errors.csv b/telethon_generator/data/errors.csv index 61669f8c..6ccd4667 100644 --- a/telethon_generator/data/errors.csv +++ b/telethon_generator/data/errors.csv @@ -171,7 +171,7 @@ PHOTO_CONTENT_URL_EMPTY,400,The content from the URL used as a photo appears to PHOTO_CROP_SIZE_SMALL,400,Photo is too small PHOTO_EXT_INVALID,400,The extension of the photo is invalid PHOTO_INVALID,400,Photo invalid -PHOTO_INVALID_DIMENSIONS,400,The photo dimensions are invalid +PHOTO_INVALID_DIMENSIONS,400,The photo dimensions are invalid (hint: `pip install pillow` for `send_file` to resize images) PHOTO_SAVE_FILE_INVALID,400,The photo you tried to send cannot be saved by Telegram. A reason may be that it exceeds 10MB. Try resizing it locally PHOTO_THUMB_URL_EMPTY,400,The URL used as a thumbnail appears to be empty or has caused another HTTP error PIN_RESTRICTED,400,You can't pin messages in private chats with other people