In this article we’re going to learn how to recognize the text from a picture using Python and orc.space API.
OCR (Optical character recognition) is the process by which the computer recognizes the text from an image.
ocr.space is an OCR engine that offers free API.
It means that is going to do pretty much all the work regarding text detection. We only need to send through their API an image with the text we want to scan, and it will return us the text scanned.
Let’s see step by step how to do that:
How to use ocr.space API?
First of all, you need to get an API key by registering to their website.
Go on http://ocr.space/OCRAPI and then click on “Register for free API Key”.
Once you have the key, follow the steps below.
1) Import the libraries and load the image
Let’s import all the libraries that we need (Opencv, IO, numpy, requests, json).
IO and Json are by default already installed on python, you should install the other libraries, if you haven’t done it yet.
Then we load the image.

import io
import json
import cv2
import numpy as np
import requests
img = cv2.imread("screenshot.jpg")
height, width, _ = img.shape
We can cut the image to select only the area where there is the text, in case the image contains some background.
In this case from the imagein example i’m going to cut the left side where there is some unrecognizable text from the left page of the book.

# Cutting image roi = img[0: height, 400: width]
2) Set the OCR engine
We now have the image and our goal is to send the image to the orc.space server in order to be processed.
On line 14 we have the url of ocr.space api where we need to send our image.
On line 15 we’re going to compress the image in JPG format. The simple reason for this compression is that using the free service we can send image with maximum 1mb of size, so this compression will shrink the size of our image.
On line 16 we convert the image into bytes. It must be converted to bytes to be sent to the server.
# Ocr
url_api = "https://api.ocr.space/parse/image"
_, compressedimage = cv2.imencode(".jpg", roi, [1, 90])
file_bytes = io.BytesIO(compressedimage)
Later we send the bytes to the server using the python library requests.
We need to pass three parameters:
- the first is the url_api
- Called “Files” which contains the name of the file and the file bytes we generated before after we compressed the image.
- And then “Data” which contains the post parameters of the OCR engine.
We need to insert the api key where now it’s written “YOURAPIKEYHERE”, and language is the language of our text. By default is english.
Go on this page http://ocr.space/OCRAPI to see all the “POST parameters” you can use.
The function is going to send the image to the server and in return we’re going to get the response from the server.
result = requests.post(url_api,
files = {"screenshot.jpg": file_bytes},
data = {"apikey": "YOURAPIKEYHERE",
"language": "eng"})
3) Read the Result
The result from the server is a string.
We’re going first of all to extract the content of result, then we convert the content into a dictionary.
result = result.content.decode() result = json.loads(result)
Result contains the text read from the OCR engine plus a few other values. The other values depend from the post parameters we did set before.
For example if we enabled the text Overlay we would get the coordinates with the position of the text in the image.
Now from the result let’s extract only the text as it is our focus on this project:
parsed_results = result.get("ParsedResults")[0]
text_detected = parsed_results.get("ParsedText")
print(text_detected)
I hope the article was useful to you. If you are interested in the topic I suggest you also look at Text recognition (OCR) with Tesseract and OpenCV.

Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Developers to build efficient computer vision software.
Awesome work
Hi,
I have try to generate API key through FREE OCR API website(https://ocr.space/ocrapi) through the mention link and has been generated successfully, but while i has used that key in my program it throws an error like: “API Key is not specified. Please provide a valid API key.”
Could please help me out.
thanks brother:)
hello thanks for the code it’s awesome. Is there a way to work woith multiple images
Yes of course you can do this for how many images you want.
You should load multiple images and then process them in a loop.
Hi Artheamias,
How you have generated that free API key, because i have try to generate a key(It generated successfully) but it show an error that “API Key is not specified. Please provide a valid API key”.
Don’t know why it throws an that error.
Could you please suggest solution.
Hi! I have a question. Can we read or traine this API to read icons or symbols?
Why I am getting this error?
“You may only perform this action upto maximum 10 number of times within 86400 seconds”
You need to register an account on ocr.space and get an API key to upgrade the limit. There is a limit of maximum 10 requests for non registered users.
Tutorial is very helpful, if i want to ocr PDF document, without converting image can i do OCR