ocr.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import os
  21. from pyrogram import filters
  22. from pyrogram.errors.exceptions.bad_request_400 import MessageEmpty
  23. from wbb import app, arq
  24. from wbb.core.decorators.errors import capture_err
  25. from wbb.utils.functions import transfer_sh
  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 (
  34. not reply.document
  35. and not reply.photo
  36. and not reply.sticker
  37. and not reply.animation
  38. and not reply.video
  39. ):
  40. await message.reply_text(
  41. "Reply to an image/document/sticker/animation to perform OCR on it."
  42. )
  43. return
  44. m = await message.reply_text("Scanning")
  45. if reply.document:
  46. if int(reply.document.file_size) > 3145728:
  47. await m.edit("File too large")
  48. return
  49. mime_type = reply.document.mime_type
  50. if mime_type != "image/png" and mime_type != "image/jpeg":
  51. await m.edit("Document Mimetype Invalid")
  52. return
  53. file_id = reply.document.file_id
  54. if reply.sticker:
  55. if reply.sticker.is_animated:
  56. if not reply.sticker.thumbs:
  57. await m.edit("Sticker Has No Thumb")
  58. return
  59. file_id = reply.sticker.thumbs[0].file_id
  60. else:
  61. file_id = reply.sticker.file_id
  62. if reply.photo:
  63. file_id = reply.photo.file_id
  64. if reply.animation:
  65. if not reply.animation.thumbs:
  66. await m.edit("Gif Has No Thumbnail, so it cannot be scanned for OCR")
  67. return
  68. file_id = reply.animation.thumbs[0].file_id
  69. if reply.video:
  70. if not reply.video.thumbs:
  71. return
  72. file_id = reply.video.thumbs[0].file_id
  73. file = await app.download_media(file_id)
  74. url = await transfer_sh(file)
  75. os.remove(file)
  76. data = await arq.ocr(url)
  77. try:
  78. await m.edit(data.ocr)
  79. except MessageEmpty:
  80. await m.edit("There's no text in that image.")
  81. return