Created
June 6, 2023 17:35
-
-
Save itssoap/9206f6cab976e9ff930d70038149cd52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from telethon.sync import TelegramClient, events | |
from io import BytesIO | |
from PIL import Image | |
api_id = '' | |
api_hash = '' | |
phone_number = '' | |
destination_channel_username = '@eternalcropped' | |
client = TelegramClient('session_name', api_id, api_hash) | |
client.start(phone_number) | |
def crop_image(image): | |
image = Image.open(image) | |
width, height = image.size | |
left = int(width * 0.042) | |
top = int(height * 0.042) | |
right = int(width * 0.958) | |
bottom = int(height * 0.958) | |
cropped_image = image.crop((left, top, right, bottom)) | |
return cropped_image | |
@client.on(events.NewMessage(chats = [-1001541543072])) | |
async def handler(event): | |
print("Event Occured") | |
if event.media and hasattr(event.media, 'photo'): | |
photo = event.media.photo | |
image = await client.download_media(photo, bytes) | |
image = crop_image(BytesIO(image)) | |
if image: | |
cropped_image_file = BytesIO() | |
image.save(cropped_image_file, format='PNG') | |
cropped_image_file.seek(0) | |
await client.send_file(destination_channel_username, cropped_image_file, caption=event.text or None) | |
client.start() | |
client.run_until_disconnected() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment