InstagramSizeBot: Empowering Instagram Posts with Square Photos and Small Frames

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)

 

See also  Matplotlib image from Django Model to template

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:

  1. Importing necessary libraries:
    • aiogram: A Python library for building Telegram bots.
    • Image from PIL: A class from the Python Imaging Library used for image processing.
    • BytesIO from io: A class that allows working with in-memory binary data as files.
    • executor from aiogram.utils: A module that provides functions for executing the bot.
  2. Setting up the bot:
    • TOKEN: A string variable representing the Telegram bot token.
    • Creating an instance of the aiogram.Bot class, passing the token.
    • Creating an instance of the aiogram.Dispatcher class, passing the bot instance.
  3. Defining a message handler:
    • The @dp.message_handler decorator is used to register a function as a handler for incoming messages.
    • content_types=aiogram.types.ContentTypes.PHOTO specifies that the handler should only be triggered for photo messages.
    • The process_photo function 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_id method.
      • Opens the downloaded image using PIL’s Image.open method.
      • 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 BytesIO object 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_photo method.
  4. Starting the bot:
    • The executor.start_polling function is called to start the bot’s event loop and listen for incoming updates from Telegram.
    • The dp argument is the Dispatcher instance that handles the updates.
    • skip_updates=True is used to ignore any missed updates when starting the bot.
See also  Convert image to Telegram sticker bot

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).

@InstagramSizeBot

 

Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *