Step#1: Scale the image
Step#2: Apply adaptive-threshold
Step#3: Set page-segmentation-mode (psm
) to 6 (Assume a single uniform block of text.)
1 Scaling the image:
The reason is to see the image clearly, since the original image is really small.
-
img = cv2.imread("udQw1.png")
img = cv2.resize(img, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
2 Apply adaptive-threshold
Generally threshold
is applied, but in your image, applying threshold
has no effect to the result.
For different images you may need to set different C
and block
values.
For instance for the 1st image:
-
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 15, 22)
Result:
For instance for the 2nd image:
-
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 51, 4)
Result:
3 Set psm
to 6 which assumes the image as a single uniform block of text.
-
txt = pytesseract.image_to_string(thr, config="--psm 6")
print(txt)
Result for the 1st image:
-
Dragon Claymore
1,388,888,888 mesos.
Maple Pyrope Spear
288,888,888 mesos.
Element Pierce
488,888,888 mesos.
Purple Adventurer Cape
97,777,777 mesos.
Result for the 2nd image:
-
Ring of Alchemist
749,999,995 mesos.
Dragon Slash Claw
499,999,995 mesos.
"Stormcaster Gloves
149,999,995 mesos.
Elemental Wand 6
749,999,995 mesos.
Big Money Chalr
1 tor 249,999,985 mesos.|
Code for the 1st image:
import pytesseract
import cv2
img = cv2.imread("udQw1.png")
img = cv2.resize(img, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 15, 22)
txt = pytesseract.image_to_string(thr, config="--psm 6")
print(txt)
Code for the 2nd image:
import pytesseract
import cv2
img = cv2.imread("7Y2yx.png")
img = cv2.resize(img, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 51, 4)
txt = pytesseract.image_to_string(thr, config="--psm 6")
print(txt)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…