ocr.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. import os
  25. @app.on_message(filters.command("ocr"))
  26. @capture_err
  27. async def image_ocr(_, message):
  28. if not message.reply_to_message:
  29. await message.reply_text("Reply to an image or document to perform OCR on it.")
  30. return
  31. reply = message.reply_to_message
  32. if not reply.document and not reply.photo and not reply.sticker and not reply.animation and not reply.video:
  33. await message.reply_text("Reply to an image/document/sticker/animation to perform OCR on it.")
  34. return
  35. m = await message.reply_text("Scanning")
  36. if reply.document:
  37. if int(reply.document.file_size) > 3145728:
  38. await m.edit("File too large")
  39. return
  40. mime_type = reply.document.mime_type
  41. if mime_type != "image/png" and mime_type != "image/jpeg":
  42. await m.edit("Document Mimetype Invalid")
  43. return
  44. file_id = reply.document.file_id
  45. if reply.sticker:
  46. if reply.sticker.is_animated:
  47. if not reply.sticker.thumbs:
  48. await m.edit("Sticker Has No Thumb")
  49. return
  50. file_id = reply.sticker.thumbs[0].file_id
  51. else:
  52. file_id = reply.sticker.file_id
  53. if reply.photo:
  54. file_id = reply.photo.file_id
  55. if reply.animation:
  56. if not reply.animation.thumbs:
  57. await m.edit("Gif Has No Thumbnail, so it cannot be scanned for OCR")
  58. return
  59. file_id = reply.animation.thumbs[0].file_id
  60. if reply.video:
  61. if not reply.video.thumbs:
  62. return
  63. file_id = reply.video.thumbs[0].file_id
  64. file = await app.download_media(file_id)
  65. url = await transfer_sh(file)
  66. os.remove(file)
  67. data = await arq.ocr(url)
  68. if not data.ocr:
  69. await m.edit("There's no text in that image.")
  70. return
  71. await m.edit(data.ocr)