Freetype library not found问题详解,python(编程语言)+freetype(字体引擎)opencv 中文显示

环境配置

win7 +python2.7.13+freetype-py-1.0.2

现象

win7环境下pip下载的freetype-py-1.0.2是linux的包,安装后,不能正常使用

原因

pip install freetype-py安装freetype-py-1.0.2后,dll在pip.exe同级目录的Lib\site-packages 文件夹下

cmd

C:\ProgramData\Anaconda2>pip install freetype-py
Collecting freetype-py
Installing collected packages: freetype-py
Successfully installed freetype-py-1.0.2
C:\ProgramData\Anaconda2>python
Python 2.7.13 |Anaconda 4.4.0 (32-bit)| (default, May 11 2017, 14:07:41) [MSC v.
1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import freetype
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Anaconda2\lib\site-packages\freetype\__init__.py", line 2
1, in <module>
    from freetype.raw import *
  File "C:\ProgramData\Anaconda2\lib\site-packages\freetype\raw.py", line 37, in
 <module>
    raise RuntimeError('Freetype library not found')
RuntimeError: Freetype library not found
raw.py

line 37>if platform.system() == 'Windows':

# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
#  FreeType high-level python API - Copyright 2011-2015 Nicolas P. Rougier
#  Distributed under the terms of the new BSD license.
# -----------------------------------------------------------------------------
'''
Freetype raw API

This is the raw ctypes freetype binding.
'''
import os
import platform
from ctypes import *
import ctypes.util

from freetype.ft_types import *
from freetype.ft_enums import *
from freetype.ft_errors import *
from freetype.ft_structs import *

# on windows all ctypes does when checking for the library
# is to append .dll to the end and look for an exact match
# within any entry in PATH.
filename = ctypes.util.find_library('freetype')

if filename is None:
    if platform.system() == 'Windows':
        # Check current working directory for dll as ctypes fails to do so
        filename = os.path.join(os.path.realpath('.'), 'freetype.dll')
    else:
        filename = 'libfreetype.so.6'

try:
    _lib = ctypes.CDLL(filename)
except (OSError, TypeError):
    _lib = None
    raise RuntimeError('Freetype library not found')

FT_Init_FreeType       = _lib.FT_Init_FreeType
FT_Done_FreeType       = _lib.FT_Done_FreeType
FT_Library_Version     = _lib.FT_Library_Version

try:
    FT_Library_SetLcdFilter= _lib.FT_Library_SetLcdFilter
except AttributeError:
    def FT_Library_SetLcdFilter (*args, **kwargs):
        return 0
try:
    FT_Library_SetLcdFilterWeights = _lib.FT_Library_SetLcdFilterWeights
except AttributeError:
    pass

FT_New_Face            = _lib.FT_New_Face
FT_New_Memory_Face     = _lib.FT_New_Memory_Face
FT_Open_Face           = _lib.FT_Open_Face
FT_Attach_File         = _lib.FT_Attach_File
FT_Attach_Stream       = _lib.FT_Attach_Stream

try:
    FT_Reference_Face      = _lib.FT_Reference_Face
except AttributeError:
	pass

FT_Done_Face           = _lib.FT_Done_Face
FT_Done_Glyph          = _lib.FT_Done_Glyph
FT_Select_Size         = _lib.FT_Select_Size
FT_Request_Size        = _lib.FT_Request_Size
FT_Set_Char_Size       = _lib.FT_Set_Char_Size
FT_Set_Pixel_Sizes     = _lib.FT_Set_Pixel_Sizes
FT_Load_Glyph          = _lib.FT_Load_Glyph
FT_Load_Char           = _lib.FT_Load_Char
FT_Set_Transform       = _lib.FT_Set_Transform
FT_Render_Glyph        = _lib.FT_Render_Glyph
FT_Get_Kerning         = _lib.FT_Get_Kerning
FT_Get_Track_Kerning   = _lib.FT_Get_Track_Kerning
FT_Get_Glyph_Name      = _lib.FT_Get_Glyph_Name
FT_Get_Glyph           = _lib.FT_Get_Glyph

FT_Glyph_Get_CBox      = _lib.FT_Glyph_Get_CBox

FT_Get_Postscript_Name = _lib.FT_Get_Postscript_Name
FT_Get_Postscript_Name.restype = c_char_p
FT_Select_Charmap      = _lib.FT_Select_Charmap
FT_Set_Charmap         = _lib.FT_Set_Charmap
FT_Get_Charmap_Index   = _lib.FT_Get_Charmap_Index
FT_Get_CMap_Language_ID= _lib.FT_Get_CMap_Language_ID
FT_Get_CMap_Format     = _lib.FT_Get_CMap_Format
FT_Get_Char_Index      = _lib.FT_Get_Char_Index
FT_Get_First_Char      = _lib.FT_Get_First_Char
FT_Get_Next_Char       = _lib.FT_Get_Next_Char
FT_Get_Name_Index      = _lib.FT_Get_Name_Index
FT_Get_SubGlyph_Info   = _lib.FT_Get_SubGlyph_Info

try:
    FT_Get_FSType_Flags    = _lib.FT_Get_FSType_Flags
    FT_Get_FSType_Flags.restype  = c_ushort
except AttributeError:
	pass

FT_Get_X11_Font_Format = _lib.FT_Get_X11_Font_Format
FT_Get_X11_Font_Format.restype = c_char_p

FT_Get_Sfnt_Name_Count = _lib.FT_Get_Sfnt_Name_Count
FT_Get_Sfnt_Name       = _lib.FT_Get_Sfnt_Name
FT_Get_Advance         = _lib.FT_Get_Advance


FT_Outline_GetInsideBorder  = _lib.FT_Outline_GetInsideBorder
FT_Outline_GetOutsideBorder = _lib.FT_Outline_GetOutsideBorder
FT_Outline_Get_BBox         = _lib.FT_Outline_Get_BBox
FT_Outline_Get_CBox         = _lib.FT_Outline_Get_CBox
FT_Stroker_New              = _lib.FT_Stroker_New
FT_Stroker_Set              = _lib.FT_Stroker_Set
FT_Stroker_Rewind           = _lib.FT_Stroker_Rewind
FT_Stroker_ParseOutline     = _lib.FT_Stroker_ParseOutline
FT_Stroker_BeginSubPath     = _lib.FT_Stroker_BeginSubPath
FT_Stroker_EndSubPath       = _lib.FT_Stroker_EndSubPath
FT_Stroker_LineTo           = _lib.FT_Stroker_LineTo
FT_Stroker_ConicTo          = _lib.FT_Stroker_ConicTo
FT_Stroker_CubicTo          = _lib.FT_Stroker_CubicTo
FT_Stroker_GetBorderCounts  = _lib.FT_Stroker_GetBorderCounts
FT_Stroker_ExportBorder     = _lib.FT_Stroker_ExportBorder
FT_Stroker_GetCounts        = _lib.FT_Stroker_GetCounts
FT_Stroker_Export           = _lib.FT_Stroker_Export
FT_Stroker_Done             = _lib.FT_Stroker_Done
FT_Glyph_Stroke             = _lib.FT_Glyph_Stroke
FT_Glyph_StrokeBorder       = _lib.FT_Glyph_StrokeBorder
FT_Glyph_To_Bitmap          = _lib.FT_Glyph_To_Bitmap

解决办法

目录C:\ProgramData\Anaconda2\Lib\site-packages\freetype下的raw.py文件修改导入freetype.dll的路径

filename = os.path.join(os.path.realpath('.'), 'freetype.dll')

#路径是不freetype.dll实际安装的路径

调整为

filename = "C:\\ProgramData\\Anaconda2\\Lib\\site-packages\\freetype.dll"
请根据实际 freetype.dll位置配置路径。

验证

方法一

cmd中输入python
>python
>import freetype
执行成功说明配置成功。

方法二

1.建立文件

C:\ProgramData\Anaconda2目录下建立 ft.py文件(编码utf-8)
import freetype

face = freetype.Face("msyh.ttf")#微软雅黑
face.set_char_size( 48*64 )
face.load_char('S')
bitmap = face.glyph.bitmap
print bitmap.buffer

2.执行

cmd进入C:\ProgramData\Anaconda2目录,
>python ft.py

3.执行成功


4.执行输出

C:\ProgramData\Anaconda2>python ft.py
[0, 0, 0, 0, 0, 0, 0, 55, 122, 173, 215, 233, 248, 248, 234, 215, 170, 119, 35,
0, 0, 0, 0, 0, 0, 0, 0, 5, 110, 216, 255, 255, 255, 255, 255, 255, 255, 255, 255
, 255, 255, 254, 183, 33, 0, 0, 0, 0, 0, 59, 215, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 244, 0, 0, 0, 0, 88, 249, 255, 255,
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0
, 0, 66, 254, 255, 255, 255, 255, 254, 182, 98, 46, 16, 3, 13, 28, 59, 108, 168,
 245, 255, 255, 0, 0, 12, 226, 255, 255, 255, 255, 215, 50, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 12, 108, 234, 0, 0, 97, 255, 255, 255, 255, 222, 17, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 184, 255, 255, 255, 255, 89, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 224, 255, 255, 255, 255, 21, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 248, 255, 255, 255, 255, 7, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 255, 255, 255, 255, 43, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 255, 255, 255, 255, 139, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134, 255, 255, 255, 255, 250, 71, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 250, 255, 255, 255, 255, 251, 121, 4, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 129, 255, 255, 255, 255, 255, 255, 211,
 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 169, 255, 255, 255, 255, 255,
255, 255, 189, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 142, 255, 255, 255,
 255, 255, 255, 255, 255, 191, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 22
4, 255, 255, 255, 255, 255, 255, 255, 255, 196, 67, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 10, 132, 245, 255, 255, 255, 255, 255, 255, 255, 255, 177, 34, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 143, 246, 255, 255, 255, 255, 255, 255, 255, 247,
 105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 138, 242, 255, 255, 255, 255, 2
55, 255, 255, 164, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 133, 244, 255,
255, 255, 255, 255, 255, 174, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 1
57, 255, 255, 255, 255, 255, 255, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 80, 241, 255, 255, 255, 255, 247, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 66, 249, 255, 255, 255, 255, 131, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 145, 255, 255, 255, 255, 203, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 45, 255, 255, 255, 255, 238, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 9, 255, 255, 255, 255, 247, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 22, 255, 255, 255, 255, 222, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 85, 255, 255, 255, 255, 187, 215, 62, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 6, 210, 255, 255, 255, 255, 106, 255, 255, 187, 77, 5, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 26, 189, 255, 255, 255, 255, 239, 20, 255, 255, 255, 255
, 238, 176, 115, 59, 34, 12, 4, 15, 43, 87, 160, 246, 255, 255, 255, 255, 255, 9
5, 0, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 255, 255, 255, 255, 255, 132, 0, 0, 53, 215, 255, 255, 255, 255, 255, 255, 255,
 255, 255, 255, 255, 255, 255, 255, 255, 255, 238, 98, 0, 0, 0, 0, 5, 96, 202, 2
55, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 230, 137, 18, 0, 0, 0
, 0, 0, 0, 0, 0, 31, 96, 160, 207, 232, 251, 243, 229, 212, 171, 125, 64, 2, 0,
0, 0, 0, 0, 0]

C:\ProgramData\Anaconda2>




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值