ocr.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. MIT License
  3. Copyright (c) 2021 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 without 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 wbb import app, arq
  21. from wbb.core.decorators.errors import capture_err
  22. from wbb.utils.functions import transfer_sh
  23. from pyrogram import filters
  24. from random import randint
  25. import os
  26. @app.on_message(filters.command("ocr"))
  27. @capture_err
  28. async def image_ocr(_, message):
  29. if not message.reply_to_message:
  30. await message.reply_text("Reply to an image or document to perform OCR on it.")
  31. return
  32. reply = message.reply_to_message
  33. if not reply.document and not reply.photo and not reply.sticker and not reply.animation and not reply.video:
  34. await message.reply_text("Reply to an image/document/sticker/animation to perform OCR on it.")
  35. return
  36. m = await message.reply_text("Scanning")
  37. if reply.document:
  38. if int(reply.document.file_size) > 3145728:
  39. await m.edit("File too large")
  40. return
  41. mime_type = reply.document.mime_type
  42. if mime_type != "image/png" and mime_type != "image/jpeg":
  43. await m.edit("Document Mimetype Invalid")
  44. return
  45. file_id = reply.document.file_id
  46. if reply.sticker:
  47. if reply.sticker.is_animated:
  48. if not reply.sticker.thumbs:
  49. await m.edit("Sticker Has No Thumb")
  50. return
  51. file_id = reply.sticker.thumbs[0].file_id
  52. else:
  53. file_id = reply.sticker.file_id
  54. if reply.photo:
  55. file_id = reply.photo.file_id
  56. if reply.animation:
  57. if not reply.animation.thumbs:
  58. await m.edit("Gif Has No Thumbnail, so it cannot be scanned for OCR")
  59. return
  60. file_id = reply.animation.thumbs[0].file_id
  61. if reply.video:
  62. if not reply.video.thumbs:
  63. return
  64. file_id = reply.video.thumbs[0].file_id
  65. filename = f"{randint(1000, 10000)}.jpg"
  66. file = await app.download_media(file_id)
  67. url = await transfer_sh(file)
  68. os.remove(file)
  69. data = await arq.ocr(url)
  70. await m.edit(data.ocr)