繁体   English   中英

如何使用 Wand 更改图片的对比度?

[英]How do I change the contrast of a picture using Wand?

我在 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 的信心。 请问我该怎么做?

谢谢,

如果系统使用 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')

我会使用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()

结果

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM