import pytesseract
from PIL import Image
import json
def extract_text_from_image(image_path, api_key):
# Replace this function with your text extraction logic using the API or library of your choice.
# For demonstration purposes, we will use Tesseract OCR here.
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # Replace with your Tesseract path.
extracted_text = pytesseract.image_to_string(Image.open(image_path))
return extracted_text
def main():
image_path = 'path_to_your_image.jpg' # Replace with the actual path to your image.
api_key = 'your_api_key' # Replace with your API key, if applicable.
extracted_text = extract_text_from_image(image_path, api_key)
# Create a dictionary to store the extracted text in JSON format.
data = {
'extracted_text': extracted_text
}
# Convert the dictionary to a JSON string.
json_data = json.dumps(data, indent=4)
# Save the JSON data to a file.
with open('output.json', 'w') as json_file:
json_file.write(json_data)
if __name__ == "__main__":
main()