The provided program is a Python script that utilizes the aiogram library and PIL (Python Imaging Library) to process photos sent to a Telegram bot. The purpose of the bot is to assist users in creating multi-photo posts for Instagram by ensuring that all the photos have a squared aspect ratio with small frames.
Instagram has a limitation where users cannot post a mix of both landscape and portrait-oriented photos in a multi-photo post. To overcome this limitation, the bot automatically processes the incoming photos to make them squared and adds small frames to maintain the original aspect ratio.
There is a code:
import aiogram
from PIL import Image
from io import BytesIO
from aiogram.utils import executor
TOKEN = 'YOUR_BOT_TOKEN'
bot = aiogram.Bot(token="614364363636436mUe4")
dp = aiogram.Dispatcher(bot)
# Define a message handler for photo messages
@dp.message_handler(content_types=aiogram.types.ContentTypes.PHOTO)
async def process_photo(message: aiogram.types.Message):
# Download the original image
photo = message.photo[-1]
file = await bot.download_file_by_id(photo.file_id)
# Open the image using PIL
image = Image.open(BytesIO(file.read()))
# Calculate the new dimensions
width, height = image.size
new_size = max(width, height)
if int(width) > int(height):
new_width = int(new_size * 1.01)
new_height = new_size
else:
new_height = int(new_size * 1.01)
new_width = new_size
# Calculate the positions for pasting the original image
paste_x = int((new_width - width) / 2)
paste_y = int((new_height - height) / 2)
# Create a new image with the squared dimensions
new_image = Image.new("RGB", (new_width, new_height), "white")
# Paste the original image onto the new image
new_image.paste(image, (paste_x, paste_y))
# Create a buffer to save the modified image
output_buffer = BytesIO()
# Save the modified image to the buffer
new_image.save(output_buffer, format="JPEG")
# Reset the buffer's file pointer to the beginning
output_buffer.seek(0)
# Send the modified image back to the user
await bot.send_photo(message.chat.id, photo=output_buffer)
executor.start_polling(dp, skip_updates=True)
The provided code is a Python script that uses the aiogram library and PIL (Python Imaging Library) to process photos sent to a Telegram bot and perform some modifications on them. Here’s a breakdown of the code:
- Importing necessary libraries:
aiogram: A Python library for building Telegram bots.ImagefromPIL: A class from the Python Imaging Library used for image processing.BytesIOfromio: A class that allows working with in-memory binary data as files.executorfromaiogram.utils: A module that provides functions for executing the bot.
- Setting up the bot:
TOKEN: A string variable representing the Telegram bot token.- Creating an instance of the
aiogram.Botclass, passing the token. - Creating an instance of the
aiogram.Dispatcherclass, passing the bot instance.
- Defining a message handler:
- The
@dp.message_handlerdecorator is used to register a function as a handler for incoming messages. content_types=aiogram.types.ContentTypes.PHOTOspecifies that the handler should only be triggered for photo messages.- The
process_photofunction is an asynchronous function that takes a message object as an argument. - The function performs the following steps:
- Downloads the original image using the
bot.download_file_by_idmethod. - Opens the downloaded image using PIL’s
Image.openmethod. - Calculates the new dimensions for the squared image by determining the maximum size between width and height and increasing it by 1%.
- Calculates the positions for pasting the original image onto the new image, ensuring it is centered.
- Creates a new blank image with the calculated dimensions.
- Pastes the original image onto the new image.
- Creates a
BytesIOobject as an output buffer to save the modified image. - Saves the modified image to the output buffer in JPEG format.
- Sets the file pointer of the buffer to the beginning.
- Constructs a string representing the user information.
- Sends the modified image back to the user using the
bot.send_photomethod.
- Downloads the original image using the
- The
- Starting the bot:
- The
executor.start_pollingfunction is called to start the bot’s event loop and listen for incoming updates from Telegram. - The
dpargument is theDispatcherinstance that handles the updates. skip_updates=Trueis used to ignore any missed updates when starting the bot.
- The
Note: You should replace the placeholder token 'YOUR_BOT_TOKEN' with the actual token obtained from the BotFather when creating your Telegram bot. Additionally, ensure you have the required libraries installed (aiogram and Pillow).