错误的“模块"对象没有属性"freetype" [英] Error 'module' object has no attribute 'freetype'

查看:239
本文介绍了错误的“模块"对象没有属性"freetype"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码链接,但是它显示模块对象没有属性的错误,我试图通过pip install freetype进行安装,但没有任何反应.谁能指导我.

I am using this code Link but it displays error of module object has no attribute i tried to pip install freetype but nothing happened. Can anyone please guide me with this.

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模块.

If cv2.freetype does not run in python you can still use freetype-py module.

我在python2/3的opencv中为PIL库api调用编写了一个包装,该包装可以通过以下方式使用: ="nofollow noreferrer"> https://github.com/bunkahle/PILasOPENCV )

I have written a wrapper around the PIL library api calls in opencv for python2/3 which can be used in the following way: (download from 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的项目.使用

It uses the freetype-py module in the background. PILasOPENCV is actually a project for migrating old PIL projects to OPENCV. Install with

setup.py install 

pip install PILasOPENCV 

更多详细信息和测试可以在github中找到.

More details and test can be found in github.

这篇关于错误的“模块"对象没有属性"freetype"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆