python - 使用 graphviz 将有向图转换为 StringIO

标签 python python-3.x graphviz image-graphviz

有谁知道如何将 Digraph 转换为 io.StringIO png 的技巧?我能找到的唯一代码是将它保存到磁盘,但我想忽略任何磁盘使用情况,而是在内存中处理它:

from graphviz import Digraph
import io

dot = Digraph(comment='The Round Table')
dot.node('A', 'King Arthur')

# instead of this...
dot.render('test-output/round-table.gv', view=True)

# ... I need something like this:
data = io.StringIO()
dot.export_to_png(dot)

最佳答案

是这样的吗?

from graphviz import Digraph
import io

dot = Digraph(comment='The Round Table', format='gv')
dot.node('A', 'King Arthur')

data = io.StringIO()

print("writing")
data.write( dot.pipe().decode('utf-8') )

print("reading")
data.seek(0)
print(data.read())

# print(data.getvalue())


data.close()

关于python - 使用 graphviz 将有向图转换为 StringIO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53806543/

相关文章:

python - 使用正则表达式删除日期周围的括号

latex - 使用 pandoc 启用 shell 转义

tree - 家谱的 Graphviz 点边端口

python - Python 的 string .format() 可以安全地用于不受信任的格式字符串吗?

python - PyQt 布局似乎不起作用 - 为什么所有内容都被覆盖?

python - RSA pycryptodome 解密不正确

python - 缓冲区 dtype 不匹配,预期为 'SIZE_t' 但得到 'long long'

python - 在 Ubuntu 15.04 中使用 Python3 安装 opencv-python

python - 如何在 python 中使用 pip

形状=圆形的节点的大小