Понадобился простой бот который будет конвертировать любое изображение в формат webp и потом переводит его в формат base64 сохраняет в текстовый файл и отправляет пользователю.
Надеюсь у вас есть небольшой опыт работы в python и библиотеки вы сами сможете установить.
А так постараюсь оставить все в комментариях к коду или нет..
.import telebot
import os
from PIL import Image
import base64
import time
bot = telebot.TeleBot('тут ваш ТОКЕН')
@bot.message_handler(commands=['start', 'go'])
def start_handler(message):
chat_id = message.chat.id
bot.send_message(chat_id, 'Приветствие при нажатии на старт')
@bot.message_handler(content_types='photo')
def handle_file3(message):
chat_id = message.chat.id
#получаем id фотки
user_photo_id = message.photo[-1].file_id
file_photo = bot.get_file(user_photo_id)
#ищем файл с раширением
filename, file_extension = os.path.splitext(file_photo.file_path)
downloaded_file_photo = bot.download_file(file_photo.file_path)
#тут можно добавить расположение 'photo/' + user_photo_id + file_extension
src = user_photo_id + file_extension
try:
with open(src,'wb') as new_file:
new_file.write(downloaded_file_photo)
time.sleep(1)
bot.send_message(chat_id, text='Загружаю на сервер...')
im = Image.open(user_photo_id + file_extension).convert("RGB")
im.save((user_photo_id + '.webp'),'webp')
time.sleep(1)
bot.send_message(chat_id, text='Конвертирую в webp...')
time.sleep(1)
bot.send_document(chat_id, data=open(user_photo_id + '.webp', 'rb'))
with open((user_photo_id + '.webp'), "rb") as img_file:
b64_string = base64.b64encode(img_file.read())
bas64 = (b64_string.decode('utf-8'))
time.sleep(1)
f1 = open(user_photo_id + '.txt', 'a', encoding='utf-8')
f1.write(bas64)
f1.close()
time.sleep(1)
bot.send_message(chat_id, text='Конвертирую в base64 ...')
time.sleep(1)
bot.send_document(chat_id, data=open(user_photo_id + '.txt', 'rb'))
time.sleep(1)
#Подчищаем за собой
os.remove(user_photo_id + file_extension)
time.sleep(1)
os.remove(user_photo_id + '.txt')
time.sleep(1)
os.remove(user_photo_id + '.webp')
bot.send_message(chat_id, text='Чисто')
except:
bot.send_message(chat_id, text='Sorry Error')
if __name__ == '__main__':
bot.polling(none_stop=True)
Оставлю ссылку на бота, вдруг еще работает =)