【问题标题】:How do I change the contrast of a picture using Wand?如何使用 Wand 更改图片的对比度?
【发布时间】:2020-01-07 17:03:22
【问题描述】:

我在 Tesseract OCR 中使用了下面的图片:

我处理图片的代码是:

# HOCR
with image[450:6200, 840:3550] as cropped:
    imgPage = wi(image = cropped)
    imageBlob = imgPage.make_blob('png')
    horas = gerarHocr(imageBlob)

def gerarHocr(imageBlob):
    image = Image.open(io.BytesIO(imageBlob))
    markup = pytesseract.image_to_pdf_or_hocr(image, lang='por', extension='hocr', config='--psm 6')
    soup = BeautifulSoup(markup, features='html.parser')

    spans = soup.find_all('span', {'class' : 'ocrx_word'})

    listHoras = []
    ...
    return listHoras

虽然我的 OCR 有时会感到困惑,例如将 83 重复并返回 07:44/14:183 而不是 07:44/14:13

我认为,如果我使用 Wand 删除灰线,我会提高 OCR 的信心。 请问我该怎么做?

谢谢,

【问题讨论】:

  • 使用Image.level 方法。
  • 也许将它作为 numpy 数组并使用类似 img[ img > 128 ] = 255 的东西将一些灰色转换为白色

标签: python ocr tesseract python-tesseract wand


【解决方案1】:

如果系统使用 ImageMagick-6,您可以调用Image.threshold(),但可能需要先移除透明度。

with Image(filename='PWILE.png') as img:
    img.background_color = 'WHITE'
    img.alpha_channel = False
    img.threshold(threshold=0.5)
    img.save(filename='output_threshold.png')

如果您使用的是 ImageMagick-7(任何高于版本 7.0.8-41),那么 Image.auto_threshold() 将起作用。

with Image(filename='support/PWILE.png') as img:
    img.auto_threshold(method='otsu')

【讨论】:

    【解决方案2】:

    我会使用cv2 和/或numpy.array

    将浅灰色转换为白色

    img[ img > 128 ] = 255
    

    将深灰色转换为黑色

    img[ img < 128 ] = 0
    

    import cv2
    
    folder = '/home/user/images/'
    
    # read it
    img = cv2.imread(folder + 'old_img.png')
    
    # convert ot grayscale
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # reduce colors
    
    img[ img > 128 ] = 255
    img[ img < 128 ] = 0
    
    # save it    
    cv2.imwrite(folder + 'new_img.png', img)
    
    # display result
    #cv2.imshow('window', img)
    #cv2.waitKey(0) # press any key in window to close it
    #cv2.destroyAllWindows()
    

    结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多