首页 > 解决方案 > 从python增加plantUML中的图形大小?

问题描述

MWE

要在(子)文件夹中生成 PlantUML 图:/Diagrams/我使用以下 python 脚本:

from plantuml import PlantUML
import os
from os.path import abspath
from shutil import copyfile

os.environ['PLANTUML_LIMIT_SIZE'] = str(4096 * 4)                       # set max with to 4 times the default (16,384)

server = PlantUML(url='http://www.plantuml.com/plantuml/img/',
                          basic_auth={},
                          form_auth={}, http_opts={}, request_opts={})


diagram_dir = "./Diagrams"
#directory = os.fsencode()
for file in os.listdir(diagram_dir):
  filename = os.fsdecode(file)
  if filename.endswith(".txt"):
    server.processes_file(abspath(f'./Diagrams/{filename}'))

它用于生成例如以下test.txt文件:

@startuml

'Enforce straight lines
skinparam linetype ortho

' Set direction of graph hierarchy
Left to Right direction

' create work package data
rectangle "something something something" as ffd0
rectangle "something something something" as ffd1
rectangle "something something something something  something" as ffd2
rectangle "something something something something" as ffd3
rectangle "something something somethingsomethingsomething" as ffd4
rectangle "something something something something something something" as ffd5
rectangle "something something something something" as ffd6
rectangle "something something something " as ffd7

' Implement graph hierarchy
ffd0-->ffd1
ffd1-->ffd2
ffd2-->ffd3
ffd3-->ffd4
ffd4-->ffd5
ffd5-->ffd6
ffd6-->ffd7
@enduml

预期行为

因为我按照常见问题解答PLANTUML_LIMIT_SIZE的建议将变量设置为 16384(像素),所以我希望这会填充图表的图片,所有块并排连接,最大宽度为 4096 * 4 像素。

为了测试是否从 python 脚本设置它是否被错误地实现,我还尝试手动设置它:set PLANTUML_LIMIT_SIZE=16384期望与上一段中解释的相同行为(一张图片填充到 16384 像素)。

观察到的行为

取而代之的是PlantUML在2000张水平图片处截取图片如下图所示:

问题

如何确保 PlantUML 不会n从 python 脚本中截断像素图块(高度或宽度)?

标签: pythondiagramplantuml

解决方案


我发现防止图表被截断的最佳方法是选择 SVG 输出,而不是尝试猜测大小或选择任意大的限制。

请注意,PLANTUML_LIMIT_SIZE只有在本地运行 PlantUML 时,设置才会生效,但您使用的 Python 接口似乎会将图表发送到在线服务。我不知道该接口的内部结构,但根据文档,您应该能够通过将http://www.plantuml.com/plantuml/svg/其用作服务 URL 来获得 SVG 输出。

如果您需要 PNG 格式的最终​​图像,则需要使用其他工具对其进行转换。


推荐阅读