首页 > 解决方案 > 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无法将文件写入任何格式。如何解决这个问题?

标签: pythonjpeggraphvizdot

解决方案


我的方法:(我是win10,其他操作可以跳到第2步)

  1. 将 Graphviz 的“bin/”添加到环境路径中。

    • 我的 Graphviz 安装路径:D:\software\graphviz\Graphviz2.44.1\. 它有 4 个文件夹:“bin”、“include”、“lib”、“share”
    • 所以我添加D:\software\graphviz\Graphviz2.44.1\bin到我的环境路径
  2. 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。需要写入权限。)
  3. 将路径添加到您的代码:

# 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)
  1. 我在我的电脑上成功了。 这是结果

推荐阅读