You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac OS下Pillow无法识别Libraqm安装的技术求助

解决Pillow无法识别Libraqm导致多语种文本渲染异常的问题

问题背景

在Mac OS 12.6的Python 3.9.13 conda环境中,使用Pillow 9.2.0渲染乌尔都语(如词汇لڑكا)等需要字符连写的多语种文本时,出现字母分离、左对齐的异常。执行features.check_feature('raqm')返回False,推测Pillow未识别到已通过brew安装的Libraqm 0.9.0。

排查与修复步骤

1. 确认Libraqm安装路径

先通过brew获取Libraqm的准确安装位置:

brew --prefix libraqm

输出通常为/usr/local/opt/libraqm(Intel芯片)或/opt/homebrew/opt/libraqm(Apple芯片)。

2. 从源码重新编译安装Pillow

pip默认安装的预编译Pillow可能未关联本地Libraqm,需指定依赖路径从源码编译:

# 卸载现有Pillow
pip uninstall -y pillow

# 设置Libraqm相关环境变量
export LIBRAQM_INCLUDE_DIR=$(brew --prefix libraqm)/include
export LIBRAQM_LIBRARY_DIR=$(brew --prefix libraqm)/lib

# 源码安装Pillow
pip install pillow --no-binary :all:

3. 验证Libraqm识别状态

安装完成后,运行以下代码验证:

from PIL import features
print(features.check_feature('raqm'))

若返回True,则测试文本渲染:

from PIL import Image, ImageDraw, ImageFont

# 替换为支持乌尔都语的字体路径
font = ImageFont.truetype("NotoNaskhArabic-Regular.ttf", 40)
img = Image.new("RGB", (200, 100), "white")
draw = ImageDraw.Draw(img)
draw.text((10, 10), "لڑكا", font=font, fill="black")
img.save("urdu_rendered.png")

4. 修复conda环境路径问题

若使用conda环境,需确保brew库路径被识别:

# 添加Libraqm库路径到环境变量
export DYLD_LIBRARY_PATH=$(brew --prefix libraqm)/lib:$DYLD_LIBRARY_PATH

可将此命令写入conda环境的激活脚本(如$CONDA_PREFIX/etc/conda/activate.d/env_vars.sh),实现自动加载。

5. 检查Libraqm依赖完整性

Libraqm依赖Freetype和Harfbuzz,确认这些库已安装:

brew install freetype harfbuzz

内容的提问来源于stack exchange,提问作者Dustin Chacón

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 03:55:22