0

I'm well aware this question seems like a possible duplicate (e.g., 1, 2, 3), but I couldn't find a straight answer to my scenario anywhere. The exact flow is as follows:

  1. The Bot receives a message with a photo from a user. It then extracts and stores the file_id of the highest-quality photo out of the message.photo array (the last array item).
  2. The bot fires a getFile(file_id) request using the stored file_id, which returns a single link (NOT an array) that points to a low-quality file (slightly bigger than a thumbnail).

To summarize:

  • Using the exact file_id with getFile() returns a link to a low-quality file.
  • Using the exact file_id with sendPhoto() will send a full-size photo.

On the chance I'm missing something here, can anyone confirm that that's expected behavior? Thanks.

Shaya
  • 2,792
  • 3
  • 26
  • 35

3 Answers3

4

file_id's are different in the photo array. use file_id which has the biggest file_size and then you could get the high quality file by getFile method

  • Thanks, @konstantin, The issue I experienced was that getFile(file_id) returned a link that pointed to a low-quality photo even though the file_id I supplied represented an HQ photo – Shaya Aug 11 '21 at 12:21
1

Base on image resolution, "photo" array contains different number file_id. Use ctx.message.photo.lenght - 1 to access the last file_id for best quality.

Amir
  • 36
  • 3
1

the best quality in -1 index

    raw = message.photo[-1].file_id
    
    path =path+"/"+raw+".jpg"

    file_info = bot.get_file(raw)
    downloaded_file = bot.download_file(file_info.file_path)
    with open(path,'wb') as new_file:
        new_file.write(downloaded_file)
Ali Ganjbakhsh
  • 541
  • 7
  • 13
  • Thanks, @Ali, this part is clear to me though. The issue I experienced was that `getFile(file_id)` returned a link that pointed to a low-quality photo, even though the `file_id` I supplied represented an HQ photo. – Shaya Aug 11 '21 at 12:14
  • Hi dear, `raw = message.photo[-1].file_id ` in this line if you choose -1 index, you will get the best quality of image or file. – Ali Ganjbakhsh Aug 14 '21 at 05:52
  • Again, this part is clear to me. My question is different.. nevermind :) – Shaya Aug 15 '21 at 15:48