img_pdf.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """
  2. MIT License
  3. Copyright (c) 2023 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including witout limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from io import BytesIO
  21. from os import path, remove
  22. from time import time
  23. import img2pdf
  24. from PIL import Image
  25. from pyrogram import filters
  26. from pyrogram.types import Message
  27. from wbb import app
  28. from wbb.core.decorators.errors import capture_err
  29. from wbb.core.sections import section
  30. async def convert(
  31. main_message: Message,
  32. reply_messages,
  33. status_message: Message,
  34. start_time: float,
  35. ):
  36. m = status_message
  37. documents = []
  38. for message in reply_messages:
  39. if not message.document:
  40. return await m.edit("Not document, ABORTED!")
  41. if message.document.mime_type.split("/")[0] != "image":
  42. return await m.edit("Invalid mime type!")
  43. if message.document.file_size > 5000000:
  44. return await m.edit("Size too large, ABORTED!")
  45. documents.append(await message.download())
  46. for img_path in documents:
  47. img = Image.open(img_path).convert("RGB")
  48. img.save(img_path, "JPEG", quality=100)
  49. pdf = BytesIO(img2pdf.convert(documents))
  50. pdf.name = "wbb.pdf"
  51. if len(main_message.command) >= 2:
  52. names = main_message.text.split(None, 1)[1]
  53. if not names.endswith(".pdf"):
  54. pdf.name = names + ".pdf"
  55. else:
  56. pdf.name = names
  57. elapsed = round(time() - start_time, 2)
  58. await main_message.reply_document(
  59. document=pdf,
  60. caption=section(
  61. "IMG2PDF",
  62. body={
  63. "Title": pdf.name,
  64. "Size": f"{pdf.__sizeof__() / (10 ** 6)}MB",
  65. "Pages": len(documents),
  66. "Took": f"{elapsed}s",
  67. },
  68. ),
  69. )
  70. await m.delete()
  71. pdf.close()
  72. for file in documents:
  73. if path.exists(file):
  74. remove(file)
  75. @app.on_message(filters.command("pdf"))
  76. @capture_err
  77. async def img_to_pdf(_, message: Message):
  78. reply = message.reply_to_message
  79. if not reply:
  80. return await message.reply(
  81. "Reply to an image (as document) or group of images."
  82. )
  83. m = await message.reply_text("Converting..")
  84. start_time = time()
  85. if reply.media_group_id:
  86. messages = await app.get_media_group(
  87. message.chat.id,
  88. reply.id,
  89. )
  90. return await convert(message, messages, m, start_time)
  91. return await convert(message, [reply], m, start_time)