How to get the bytes of a photo file from Telegram without saving the file?

195 views Asked by At

I'm working on a telegram bot that receives a photo with a barcode. I interact with the Telegram API through the Telebot module The photo is processed, the barcode is recognized and stored in the database. Stuck in one place. Photos from telegram are downloaded in byte format. This photo can be saved in png format and then processed and recognized by the barcode using the opencv module. But the openCV module already works with the DOWNLOADED image. How to pass a byte data type to an openCV module WITHOUT SAVING an image?

I formatted the byte code into a numpy array. But then opencv cannot see and recognize the barcode. I saved the image. Then I opened it through openCV and the barcode was recognized. But I want to transfer the image without SAVING.

your text

import telebot as tb
from config import token
import scanner
import cv2
import numpy as np
from pyzbar.pyzbar import decode
import base64
import PIL.Image as Image
import io


bot = tb.TeleBot(token)
bot.set_my_commands([
    tb.types.BotCommand("/start", "Start")])


@bot.message_handler(commands=['start'])
def start_bot(message):
    bot.send_message(message.chat.id, 'Start Bot')

@bot.message_handler(func=lambda m: True, content_types=['photo'])
def get_broadcast_picture(message):
    file_path = bot.get_file(message.photo[1].file_id).file_path
    file = bot.download_file(file_path)
    result_code = scanner_bar_cv2(file)
    bot.send_message(message.chat.id, result_code)

def scanner_bar_cv2(name_file: bytes):
    nparr = np.fromstring(name_file, np.uint8)
    r = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
    # img = cv2.imread(path)
    bd = cv2.barcode.BarcodeDetector()
    img = bd.detectAndDecode(r)



if __name__ == "__main__":
    bot.polling(non_stop=True, timeout=123)
0

There are 0 answers