问题
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)
回答1:
If cv2.freetype does not run in python you can still use freetype-py module.
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)
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
or
pip install PILasOPENCV
More details and test can be found in github.
回答2:
- install harfbuzz and freetype follow this link
- build opencv-contrib follow this link and this link
Put a symlink to the bindings in your local site-packages:
ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so $HOME/.pyenv/versions/3.5.2/lib/python3.5/site-packages/
回答3:
You're just missing opencv-contrib
, you can install that with pip install opencv-contrib-python
.
来源:https://stackoverflow.com/questions/47726854/error-module-object-has-no-attribute-freetype