Error 'module' object has no attribute 'freetype'
Asked Answered
H

5

7

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)
Health answered 9/12, 2017 at 8:42 Comment(7)
Which version of opencv do you have installed? Check import cv2; print(cv2.__version__)Mellman
As far as I can tell, the freetype module was added in opencv 3.2.0.Mellman
it is python 3.3.0Health
Not Python version. OpenCV version ? try the commands in @WarrenWeckesser 's comments.Paisley
sorry its opecv 3.3.0Health
Do you have a file called "cv2.py" in your working directory? If so, change its name.Mellman
No cv2 is not in this file directoryHealth
M
3

I find opencv-contrib-python-4.5.3.56 does not have freetype. I downgraded it to version 4.4.0.46, then it works.

pip3 install opencv-contrib-python==4.4.0.46
Madalynmadam answered 12/10, 2021 at 23:48 Comment(1)
this did not work for meThearchy
B
2

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.

Barbbarba answered 24/3, 2019 at 13:40 Comment(1)
I get this error: Traceback (most recent call last): File "/Users/jonas/Projects/SudokuSolver/SudokuDetectorPy/genTestData2.py", line 16, in <module> draw.text((30, 70), text, font=font, fill=255) File "/Users/jonas/anaconda3/lib/python3.6/site-packages/PILasOPENCV.py", line 2102, in text ink, fill = self._getink(fill) File "/Users/jonas/anaconda3/lib/python3.6/site-packages/PILasOPENCV.py", line 1795, in _getink if not self.mode[0] in ("1", "L", "I", "F") and isinstance(ink, numbers.Number): TypeError: 'NoneType' object is not subscriptableUnderfur
E
1

You're just missing opencv-contrib, you can install that with pip install opencv-contrib-python.

Efik answered 18/9, 2019 at 3:11 Comment(3)
Didn't work for me. On Ubuntu 20.10, I did sudo apt install python3-9 then I made sure that import cv2 didn't work (not installed) then I did python3.9 -m pip install opencv-contrib-python and I could do import cv2 but then cv2.freetype raises an AttributeError: module 'cv2.cv2' has no attribute 'freetype'. Also opencv-contrib-python is "Unofficial pre-built CPU-only OpenCV packages for Python" it's not an extension of OpenCV, in case someone has the same impression I did.Goering
working fine for me. Python version:3.8.2 and cv2 version: 4.5.1Amphibiotic
It didn't work for me. Python version: 3.10.13 and cv2 version: 4.8.0Jenniejennifer
M
0
  1. install harfbuzz and freetype follow this link
  2. build opencv-contrib follow this link and this link
  3. 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/

Monarchist answered 27/6, 2018 at 11:34 Comment(0)
O
0

[THIS IS A WINDOWS SPECIFIC GUIDE]

I'm pretty late to this issue, but I hope this helps someone.

I've added links to an easy to follow guide at the bottom.

Here are the essential steps, you'd need to follow:

  • Install CMake & Git (if you haven't already)
  • In a folder git vcpkg (git clone https://github.com/Microsoft/vcpkg.git)
  • Install freetype and harfbuzz using vcpkg commandline
    • bootstrap-vcpkg.bat
    • vcpkg install freetype:x64-windows
    • vcpkg install harfbuzz:x64-windows
  • Locate vcpkg.cmake file and note down its path
  • Ensure C++ build tools are installed in Visual Studio 19
  • Download and extract OpenCV & OpenCV contrib
  • Create a empty dir called "build" inside the extracted OpenCV (not OpenCV-contrib)
  • Modify CMakeLists.txt inside freetype module folder in opencv-contrib (Link)
  • Modify and run the following command:
    • cmake -G "Visual Studio 16 2019" -B D:\code\downloads\opencv-4.3.0\build -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_PYTHON_SUPPORT=ON -D BUILD_opencv_python3=yes -D PYTHON_DEFAULT_EXECUTABLE=D:\anaconda3\python.exe -D OPENCV_SKIP_PYTHON_LOADER=ON -D PYTHON_LIBRARY=D:\anaconda3\libs\python36.lib -D OPENCV_EXTRA_MODULES_PATH=D:\code\downloads\opencv_contrib-4.3.0\modules -D OPEN_CV_FORCE_PYTHON_LIBS=yes -D CMAKE_TOOLCHAIN_FILE=D:\code\downloads\vcpkg\scripts\buildsystems\vcpkg.cmake
  • Use CMake GUI to make changes referenced at this link (LINK)
  • Once necessary changes are made, click Configure and Generate.
  • Building DEBUG & RELEASE versions using Visual Studio 19
  • Test the installation by importing cv2.freetype from the environment you setup.

There's a comprehensive written guide over here:

Also, I've made a video inspired by the same tutorial, which can be found here:

I would recommend watching the video and keeping this guide open on the side.

Cheers!

Outlay answered 24/4, 2020 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.