使用pytesseract识别图像中的文本 [英] use pytesseract to recognize text from image
问题描述
我需要使用pytesseract从这张图片中提取文字:
和代码:< PIL导入图片,ImageEnhance,ImageFilter
导入pytesseract
path ='pic.gif'
img = Image.open(path)
img = img.convert('RGBA')
pix = img.load()
for y in range(img.size [1] ):
表示范围内的x(img.size [0]):
如果pix [x,y] [0]< 102或pix [x,y] [1]< 102或pix [x,y] [2]< 102:
pix [x,y] =(0,0,0,255)
else:
pix [x,y] =(255,255,255,255)
img.save('temp.jpg')
text = pytesseract.image_to_string(Image.open('temp.jpg'))
#os.remove('temp.jpg')
打印(文本)
,temp.jpg是
还不错,但打印结果是,2 WW
不是正确的文字 2HHH
,那么如何删除那些黑点呢?
这是我的解决方案:
从PIL导入pytesseract
导入Image,ImageEnhance,ImageFilter
im = Image.open(temp.jpg)#第二个
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1 ')
im.save('temp2.jpg')
text = pytesseract.image_to_string(Image.open('temp2.jpg'))
print(text)
I need to use pytesseract to extract text from this picture:
and the code:
from PIL import Image, ImageEnhance, ImageFilter
import pytesseract
path = 'pic.gif'
img = Image.open(path)
img = img.convert('RGBA')
pix = img.load()
for y in range(img.size[1]):
for x in range(img.size[0]):
if pix[x, y][0] < 102 or pix[x, y][1] < 102 or pix[x, y][2] < 102:
pix[x, y] = (0, 0, 0, 255)
else:
pix[x, y] = (255, 255, 255, 255)
img.save('temp.jpg')
text = pytesseract.image_to_string(Image.open('temp.jpg'))
# os.remove('temp.jpg')
print(text)
and the "temp.jpg" is
Not bad, but the result of print is ,2 WW
Not the right text2HHH
, so how can I remove those black dots?
Here is my solution:
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
im = Image.open("temp.jpg") # the second one
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save('temp2.jpg')
text = pytesseract.image_to_string(Image.open('temp2.jpg'))
print(text)
这篇关于使用pytesseract识别图像中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!