Home  >  Q&A  >  body text

图片处理 - python PIL 图片放大的问题

尝试用pytesseract来识别一些图片:

原始图片尺寸较小,用pytesseract无法识别,开始尝试用 Mac 自带的预览工具来放大尺寸 ,发现可以正常识别了,然后尝试用 PIL 来放大图片,代码如下:

images.resize((1985, 336),Image.ANTIALIAS).save('/images', quality=95,dpi=(72, 72))

问题是,通过 PIL 放大的图片不能被pytesseract 正常识别,我对比了两张图片的信息,如下图:

发现除了图片尺寸不同外,其他信息都是相同的,但是就是无法被pytesseract正常识别,所以求教如何用 PIL 放大出可以正常被pytesseract识别的图片(其他工具也可以)

巴扎黑巴扎黑2741 days ago1074

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:31:23

    You can try changing the -psm parameter of tesseract:

    image = Image.open('ocr.png')
    image.show()
    for p in range(4,14):
        print(p, '-', pytesseract.image_to_string(image, config="-psm {}".format(p)))

    The output is as follows:

    4 - 3 4'1 4'1 I] I]
    5 - D
    D
    d.
    d.
    _...I._
    6 - 3 4'1 4'1 I] I]
    7 - 3 4'1 4'1 I] I]
    8 - 34400
    9 - 34400
    10 - W
    11 - 3
    
    £1
    
    I1
    
    I]
    
    I]
    12 - 
    13 - 34400

    If the picture only has numbers, you can also try ittessedit_char_whitelist:

    pytesseract.image_to_string(image, config="-psm 8 -c tessedit_char_whitelist=1234567890")

    reply
    0
  • Cancelreply