是否可以使用 pyocr
或 Tesseract
从图像中获取字体大小?
下面是我的代码。
tools = pyocr.get_available_tools()
tool = tools[0]
txt = tool.image_to_string(
Imagee.open(io.BytesIO(req_image)),
lang=lang,
builder=pyocr.builders.TextBuilder()
)
在这里,我使用函数 image_to_string
从图像中获取文本。现在,我的问题是,我的文本是否也可以获得 font-size
(number)。
最佳答案
使用 tesserocr ,你可以在你的图像上调用Recognize
后得到一个ResultIterator
,你可以调用WordFontAttributes
方法来获取你需要的信息。阅读该方法的文档以获取更多信息。
import io
import tesserocr
from PIL import Image
with tesserocr.PyTessBaseAPI() as api:
image = Image.open(io.BytesIO(req_image))
api.SetImage(image)
api.Recognize() # required to get result from the next line
iterator = api.GetIterator()
print iterator.WordFontAttributes()
示例输出:
{'bold': False,
'font_id': 283,
'font_name': u'Times_New_Roman',
'italic': False,
'monospace': False,
'pointsize': 9,
'serif': True,
'smallcaps': False,
'underlined': False}
关于python - 使用 Tesseract 和 Pyocr 在 Python 中获取字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39324626/