【发布时间】:2021-11-18 10:07:03
【问题描述】:
我有一个适用于某些带有数字的图像的 python 脚本,它可以正确读取它们。 工作的图像类型在这里:Working image 我正在尝试将脚本与仅带有数字的新型图像一起使用,但它不起作用。新的图片类型在这里:Non working image
我的脚本如下:
try:
from PIL import Image
from PIL import ImageEnhance
except ImportError:
import Image
import pytesseract
black = (0,0,0)
white = (255,255,255)
threshold = (160,160,160)
# Open input image in grayscale mode and get its pixels.
img = Image.open("./in/web_search.jpg").convert("LA")
# multiply each pixel by 1.2
out = img.point(lambda i: i * 1.3)
enh = ImageEnhance.Contrast(out)
enh.enhance(1.3).show("30% more contrast")
pixels = out.getdata()
newPixels = []
# Compare each pixel
for pixel in pixels:
if pixel < threshold:
newPixels.append(black)
else:
newPixels.append(white)
# Create and save new image.
newImg = Image.new("RGB",out.size)
newImg.putdata(newPixels)
newImg.save("./out/web_search.jpg")
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
print("-----------------------")
print(pytesseract.image_to_string(Image.open('./out/web_search.jpg'), lang='eng', config='--psm 10 --oem 3 -c tessedit_char_whitelist=1234567890 --tessdata-dir="/usr/share/tesseract-ocr/4.00/tessdata/"'))
print("-----------------------")
我的新图像的结果是:
-----------------------
Riemer gaat bee 6 eee
-----------------------
有什么帮助吗? 谢谢。
【问题讨论】: