always resize images

This commit is contained in:
bakatrouble 2023-08-13 22:30:21 +03:00
parent e83c970717
commit 2fd30ea5d0

32
main.py
View File

@ -83,22 +83,22 @@ async def send_post(post: E621Post, tag_list: List[str]):
src_path.unlink() src_path.unlink()
mp4_path.unlink() mp4_path.unlink()
elif post.file.ext in ('png', 'jpg'): elif post.file.ext in ('png', 'jpg'):
if post.file.size > 10000000: # if post.file.size > 10000000:
logging.warning('compressing') logging.warning('compressing')
dl_im = Image.open(file).convert('RGBA') dl_im = Image.open(file).convert('RGBA')
size = dl_im.size size = dl_im.size
if size[0] > 2000 or size[1] > 2000: if size[0] > 2000 or size[1] > 2000:
larger_dimension = max(size) larger_dimension = max(size)
ratio = 2000 / larger_dimension ratio = 2000 / larger_dimension
dl_im = dl_im.resize((int(size[0] * ratio), int(size[1] * ratio)), dl_im = dl_im.resize((int(size[0] * ratio), int(size[1] * ratio)),
Image.LANCZOS) Image.LANCZOS)
print(f'Resizing from {size[0]}x{size[1]} to {dl_im.size[0]}x{dl_im.size[1]}') print(f'Resizing from {size[0]}x{size[1]} to {dl_im.size[0]}x{dl_im.size[1]}')
im = Image.new('RGBA', dl_im.size, (255, 255, 255)) im = Image.new('RGBA', dl_im.size, (255, 255, 255))
composite = Image.alpha_composite(im, dl_im).convert('RGB') composite = Image.alpha_composite(im, dl_im).convert('RGB')
file = BytesIO() file = BytesIO()
composite.save(file, format='JPEG') composite.save(file, format='JPEG')
file.seek(0) file.seek(0)
file.name = 'file.jpg' file.name = 'file.jpg'
await bot.send_photo(int(os.environ['SEND_CHAT']), await bot.send_photo(int(os.environ['SEND_CHAT']),
file, file,
caption=caption, caption=caption,