From b51a280dc969be82f5d269481a4cc65206f59df8 Mon Sep 17 00:00:00 2001 From: YoilyL Date: Thu, 25 Apr 2019 18:08:12 +0000 Subject: [PATCH] fixed stripped image downloads --- telethon/client/downloads.py | 3 ++- telethon/utils.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/telethon/client/downloads.py b/telethon/client/downloads.py index 06e2917b..aa7008cd 100644 --- a/telethon/client/downloads.py +++ b/telethon/client/downloads.py @@ -342,7 +342,8 @@ class DownloadMethods(UserMethods): f = open(file, 'wb') else: f = file - + if isinstance(size, types.PhotoStrippedSize): + size.bytes = utils.stripped_to_photo(size.bytes) try: f.write(size.bytes) finally: diff --git a/telethon/utils.py b/telethon/utils.py index 05cf9432..4633c171 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -1123,3 +1123,17 @@ class AsyncClassWrapper: return w.fget(self.wrapped) else: return w + + +def stripped_to_photo(stripped): + """ + adds the jpg header and footer to a stripped image. + transcoded from https://github.com/telegramdesktop/tdesktop/blob/bec39d89e19670eb436dc794a8f20b657cb87c71/Telegram/SourceFiles/ui/image/image.cpp#L225 + """ + if len(stripped) < 3 or stripped[0] != '\x01': + return stripped + header = bytearray(b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00(\x1c\x1e#\x1e\x19(#!#-+(0