python - Graphviz 格式:“jpeg”无法识别
问题描述
运行以下代码(来自https://pypi.org/project/graphviz/的示例):
from graphviz import Digraph
dot = Digraph(comment='The Round Table', format='jpeg')
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')
dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false')
dot.render('test-output/round-table.gv', view=True)
结果是:
Format: "jpeg" not recognized. Use one of:
似乎graphviz无法将文件写入任何格式。如何解决这个问题?
解决方案
我的方法:(我是win10,其他操作可以跳到第2步)
将 Graphviz 的“bin/”添加到环境路径中。
- 我的 Graphviz 安装路径:
D:\software\graphviz\Graphviz2.44.1\
. 它有 4 个文件夹:“bin”、“include”、“lib”、“share” - 所以我添加
D:\software\graphviz\Graphviz2.44.1\bin
到我的环境路径
- 我的 Graphviz 安装路径:
cmd 运行命令“dot -c”进行配置。
dot
意味着D:\software\graphviz\Graphviz2.44.1\bin\dot.exe
。由于第 1 步,我只需要运行dot -c
,否则,我需要运行D:\software\graphviz\Graphviz2.44.1\bin\dot.exe -c
dot -c
表示:配置插件(使用可用的插件信息写入 $prefix/lib/graphviz/config。需要写入权限。)
将路径添加到您的代码:
# add
import os
path_graphviz = "D:/software/graphviz/Graphviz2.44.1/bin" # replace by your Graphviz bin path
os.environ["PATH"] += os.pathsep + path_graphviz
# origin code
from graphviz import Digraph
dot = Digraph(comment='The Round Table', format='jpeg')
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')
dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false')
dot.render('test-output/round-table.gv', view=True)
- 我在我的电脑上成功了。 这是结果
推荐阅读
- python-3.x - 无法在 PDF 中附加多个图像文件
- xml - Postman 中的有状态 Amadeus Soap 标头 4 请求
- c# - UWP 应用未通过认证过程
- python - 通过 Python 设置数据框样式
- python - 如何编写python脚本在每天晚上11:30自动运行?
- javascript - 在 HTML 表格中隐藏和显示内容
- json - 颤振列表
JSON 到 Widget - angular - 使用 Angular 2 进行表单验证
- ios - How to process firestore query as i am getting snapshot result as FIRQuerySnapshot`
- python - 现场录音延迟