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

如何在GraphViz中调整图片大小?图片过大问题求助

解决GraphViz中节点图片尺寸过大的问题

你现在的问题是用错了控制图片大小的参数,GraphViz里的height/width配合resize没法正确缩放图片,得换用专门的图片缩放属性:

关键修改点

  • 将节点的shape设为"none":避免默认的box形状框干扰图片的尺寸适配
  • 添加imagescale="true":让图片自动缩放以匹配你设置的height和width(也可以指定"width"或"height",强制按宽/高维度缩放)
  • 调整尺寸单位:把pt换成更直观的单位,比如英寸(GraphViz默认单位)或像素,0.05pt太小,GraphViz可能无法正确解析

修正后的代码

import graphviz

dot = graphviz.Digraph(comment='Example Graph')
# 修改shape为none,添加imagescale,调整尺寸单位
dot.node('A', 'Node A', color='#ff000042', shape='none', style='filled', fillcolor='#ff000042',
         height="1", width="1", image="image/concave.PNG", imagepos="tc",
         labelloc="b", imagescale="true")
dot.node('B', 'Node B')
dot.edge('A', 'B')

# 渲染为PNG
dot.render('example_graph', format='png')

其他可选方案

  • 如果想整体缩放整个图,在渲染时添加scale参数:dot.render('example_graph', format='png', scale=0.5)(0.5表示缩放到原尺寸的50%)
  • 提前用图片编辑工具(比如PS、系统画图工具)把concave.PNG缩小到合适尺寸,再导入GraphViz

内容的提问来源于stack exchange,提问作者Rob

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:13:25