files.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from PIL import Image
  2. from pyrogram import Client, raw
  3. from pyrogram.file_id import FileId
  4. STICKER_DIMENSIONS = (512, 512)
  5. async def resize_file_to_sticker_size(file_path: str):
  6. im = Image.open(file_path)
  7. if (im.width, im.height) < STICKER_DIMENSIONS:
  8. size1 = im.width
  9. size2 = im.height
  10. if im.width > im.height:
  11. scale = STICKER_DIMENSIONS[0]/size1
  12. size1new = STICKER_DIMENSIONS[0]
  13. size2new = size2 * scale
  14. else:
  15. scale = STICKER_DIMENSIONS[1]/size2
  16. size1new = size1 * scale
  17. size2new = STICKER_DIMENSIONS[1]
  18. size1new = math.floor(size1new)
  19. size2new = math.floor(size2new)
  20. sizenew = (size1new, size2new)
  21. im = im.resize(sizenew)
  22. else:
  23. im.thumbnail(STICKER_DIMENSIONS)
  24. im.save(file_path, "PNG")
  25. async def upload_document(client: Client, file_path: str) -> raw.base.InputDocument:
  26. media = await client.send(
  27. raw.functions.messages.UploadMedia(
  28. peer=raw.types.InputPeerEmpty(),
  29. media=raw.types.InputMediaUploadedPhoto(
  30. file=file_path
  31. )
  32. )
  33. )
  34. return raw.types.InputDocument(id=media.photo.id, access_hash=media.access_hash, file_reference=media.photo.file_reference)
  35. async def get_document_from_file_id(file_id: str) -> raw.base.InputDocument:
  36. decoded = FileId.decode(file_id)
  37. return raw.types.InputDocument(id=decoded.media_id, access_hash=decoded.access_hash, file_reference=decoded.file_reference)