26 lines
690 B
Python

import sys
import pytesseract
from PIL import Image
import json
def ocr_image_to_json(image_path):
try:
# Open the image file
image = Image.open(image_path)
# Perform OCR on the image
ocr_result = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
# Convert the OCR result to JSON
ocr_json = json.dumps(ocr_result, indent=4)
print(ocr_json)
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python ocr_to_json.py <image_path>")
else:
image_path = sys.argv[1]
ocr_image_to_json(image_path)