tencent cloud

Optical Character Recognition

How to Correct Skewed Text

PDF
Focus Mode
Font Size
Last updated: 2026-04-28 14:56:19
In practical application scenarios, text in many images is skewed due to the user's shooting angle. Tencent Cloud General Printed Character Recognition not only supports extracting text information from images but also corrects image rotation for skewed text. It returns the image's rotation angle and the corrected coordinates of text line positions, facilitating your secondary processing and utilization of the image text.

Using Parameters

You can correct skewed text in images by using the following output parameters.
Parameter Name
Type
Description
Angel
Float
The rotation angle of the image (in degrees), with the horizontal direction of the text as 0°; clockwise is positive, and counterclockwise is negative.
Polygon
Array of Coord
Coordinates of the text line, represented by the coordinates of its four vertices. Note: This field may return null, indicating that no valid value can be obtained.
ItemPolygon
Pixel coordinates of the text line in the rotation-corrected image, represented as (top-left x, top-left y, width, height).
Before rotation:

After rotation:


Usage Instruction

Step 1: Prepare an image containing skewed text.

Step 2: Call the General Printed Character Recognition API to obtain the following request response. For details on how to call it, see the Operation Guide and Quick Start Guide.
{
"TextDetections": [
{
"DetectedText": "Confetteria",
"Confidence": 99,
"ItemPolygon": {
"X": 473,
"Y": 273,
"Width": 112,
"Height": 22
},
"Polygon": [
{
"X": 450,
"Y": 211
},
{
"X": 560,
"Y": 223
},
{
"X": 558,
"Y": 244
},
{
"X": 448,
"Y": 232
}
],
"AdvancedInfo": "{\\"Parag\\":{\\"ParagNo\\":1}}"
},
{
"DetectedText": "Raffaello",
"Confidence": 99,
"ItemPolygon": {
"X": 396,
"Y": 304,
"Width": 282,
"Height": 68
},
"Polygon": [
{
"X": 370,
"Y": 233
},
{
"X": 649,
"Y": 265
},
{
"X": 642,
"Y": 331
},
{
"X": 362,
"Y": 299
}
],
"AdvancedInfo": "{\\"Parag\\":{\\"ParagNo\\":2}}"
},
...
],
"Language": "zh",
"Angel": 6.5,
"RequestId": "8f038ec7-062d-44b0-b2f9-e5736ced4f84"
}
Step 3: Perform a secondary correction of the text in the image based on the image rotation parameters. Rotate the image counterclockwise by 6.5 degrees according to the output parameter "Angle": 6.5. For the image rotation code, refer to the following Python sample:
import cv2
from PIL import Image
import numpy
def rotate_img(image,angle):
img = Image.fromarray(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
rotated = img.rotate(angle, expand = 1)
rotated = cv2.cvtColor(numpy.asarray(rotated),cv2.COLOR_RGB2BGR)
return rotated
image = cv2.imread('./demo.jpg')
angle = 6.4
rotate_img = rotate_img(image, angle)
img2 = rotate_img[:,:,::-1]
After running the process, you can obtain the rotated text image. Taking the text line "Raffaello" as an example, you can obtain the corrected text line location after rotation based on the output parameter "ItemPolygon": {"X": 396, "Y": 304, "Width": 282, "Height": 68}.



Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback