Skip to content

Instantly share code, notes, and snippets.

Created July 29, 2020 06:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Demonstrates using Python winrt to run OCR. Requires Python 3.7+.
import asyncio
import os
import winrt
from PIL import Image
from import BitmapDecoder, BitmapPixelFormat, SoftwareBitmap
from import OcrEngine
from import StorageFile, FileAccessMode
import as streams
async def load_image_file_native(file_path):
file = await StorageFile.get_file_from_path_async(os.fspath(file_path))
stream = await file.open_async(FileAccessMode.READ)
decoder = await BitmapDecoder.create_async(stream)
return await decoder.get_software_bitmap_async()
def load_image_file_pillow(file_path):
image ="RGBA")
data_writer = streams.DataWriter()
bytes = image.tobytes()
bitmap = SoftwareBitmap(BitmapPixelFormat.RGBA8, image.width, image.height)
return bitmap
async def run_ocr(bitmap):
engine = OcrEngine.try_create_from_user_profile_languages()
return await engine.recognize_async(bitmap)
async def async_main():
screenshot_path = r"C:\Users\james\Documents\OCR\logs\failure_1595912159.34.png"
# bitmap = await load_image_file_native(screenshot_path)
bitmap = load_image_file_pillow(screenshot_path)
result = await run_ocr(bitmap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment