python - 错误 'module' 对象没有属性 'freetype'

标签 python numpy opencv freetype freetype2

我正在使用此代码 Link但它显示错误 module object has no attribute 我尝试 pip install freetype 但什么也没发生。谁能指导我解决这个问题。

import cv2
import numpy as np   
img = np.zeros((100, 300, 3), dtype=np.uint8)

ft = cv2.freetype.createFreeType2()
ft.loadFontData(fontFileName='Ubuntu-R.ttf',
                id=0)
ft.putText(img=img,
           text='Quick Fox',
           org=(15, 70),
           fontHeight=60,
           color=(255,  255, 255),
           thickness=-1,
           line_type=cv2.LINE_AA,
           bottomLeftOrigin=True)

cv2.imwrite('image.png', img)

最佳答案

如果 cv2.freetype 不在 python 中运行,您仍然可以使用 freetype-py 模块。

我已经为 python2/3 的 opencv 中的 PIL 库 api 调用编写了一个包装器,可以通过以下方式使用:(从 https://github.com/bunkahle/PILasOPENCV 下载)

from __future__ import print_function
import PILasOPENCV as Image
import PILasOPENCV as ImageDraw
import PILasOPENCV as ImageFont
import cv2

font = ImageFont.truetype("arial.ttf", 30)
print(font)
im = Image.new("RGB", (512, 512), "grey")
draw = ImageDraw.Draw(im)
text = "Some text in arial"
draw.text((100, 250), text, font=font, fill=(0, 0, 0))
print(ImageFont.getsize(text, font))
mask = ImageFont.getmask(text, font)
print(type(mask))
cv2.imshow("mask", mask)
im.show()
im_numpy = im.getim()
print(type(im_numpy), im_numpy.shape, im_numpy.dtype)

它在后台使用 freetype-py 模块。 PILasOPENCV 实际上是一个将旧的 PIL 项目迁移到 OPENCV 的项目。安装

setup.py install 

pip install PILasOPENCV 

更多细节和测试可以在github中找到。

关于python - 错误 'module' 对象没有属性 'freetype',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47726854/

相关文章:

python - 如何将 pandas Series 对齐到相同的长度?

python - 访问/分段 numpy 重新数组中的列

ios - 从 Mat 创建 UIImage 后,OpenCV UIImageView setImage 不工作

opencv - 压印到金属板上的文本的 OCR

python - 如果我的输出变量是二进制,我是否需要使用 one_hot 编码?

python - 从字典列表创建嵌套字典

python - 为什么类的新实例与其他实例共享成员?

python - 属性错误: module 'dill._dill' has no attribute 'log'

python - Python 中的语法 (.T)

python - 如何使用python检测复选框