温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么使用draw.io插件在vscode中一体化导出高质量图片

发布时间:2022-08-08 15:19:46 来源:亿速云 阅读:1159 作者:iii 栏目:开发技术

本文小编为大家详细介绍“怎么使用draw.io插件在vscode中一体化导出高质量图片”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么使用draw.io插件在vscode中一体化导出高质量图片”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

vscode一体化导出drawio

需要的工具:vscode, draw.io扩展,draw.io桌面版 、python

提示:这个方法并没有简化流程,只是能够减少打开窗口,在vscode中直接查看原始文件,导出文件,效率并没有显著提升。

啰嗦的部分:
drawio流程图绘制软件比visio好用。而且vscode有插件。vs插件的优点在于支持mermaid流程图,缺点非常明显不支持指定分辨率图像导出。
网上检索发现drawio的桌面版可以在命令行操作。在drawio安装目录下运行cmd,然后通过draw.io -help 命令可以查看全部命令。
查看命令发现没有针对性的dpi设置。我就想到给图片输入宽度和高度的方式。根据我猜测这个宽度和高度应该对应的就是像素点数量,所以我就按照实际尺寸,以及dpi的定义
写了一个命令, 发现可用。但是每次都要计算宽度、高度、调用cmd命令窗口,太麻烦。功能强大的python加上插件齐全的vscode能不能全部实现呢?
折腾了几个小时,终于搞定了。

进入正题:
1、安装draw.io插件,和桌面版,记住桌面版的路径
2、绘制好的图片有一个实际的尺寸,记住实际尺寸的宽和高
3、python转换 宽度

dpivalue=600#dpi
realwidth=89.4#mm
realheight=81.2#mm
width=round(dpivalue*realwidth/25.4)
height=round(dpivalue*realheight/25.4)

4、 构造命令行,不嫌麻烦可以自己写完整目录

inputfilepath=r'F:\BaiduNetdiskWorkspace\00typora\大论文\drawio\算法流程图.drawio'
outputfilepath=r'F:\BaiduNetdiskWorkspace\00typora\大论文\drawio\test.png'
starttext=r'draw.io -x '
midletext=r" -f png -t --width "+str(width)+r" --height " +str(height)+r" -o "
commandtext1=starttext+inputfilepath+midletext+outputfilepath

5、 改变python文件工作目录并执行命令

path="D:/draw.io/"#安装文件夹
os.chdir( path )# 修改当前工作目录
v2=os.system(commandtext1)

完整的代码

代码量并不大,非常简单

'''
Author: zzx
Date: 2022-07-27 10:12:38
LastEditors: zdl
LastEditTime: 2022-07-30 16:10:22
FilePath: \matlabcodef:\BaiduNetdiskWorkspace\00typora\大论文\drawio\output.py
Description: 

Copyright (c) 2022 by zdl, All Rights Reserved. 
'''
import os
dpivalue=600#dpi
realwidth=89.4#mm
realheight=81.2#mm
width=round(dpivalue*realwidth/25.4)
height=round(dpivalue*realheight/25.4)
inputfilepath=r'F:\BaiduNetdiskWorkspace\00typora\大论文\drawio\算法流程图.drawio'
outputfilepath=r'F:\BaiduNetdiskWorkspace\00typora\大论文\drawio\test.png'
starttext=r'draw.io -x '
midletext=r" -f png -t --width "+str(width)+r" --height " +str(height)+r" -o "
commandtext1=starttext+inputfilepath+midletext+outputfilepath
path="D:/draw.io/"#安装文件夹
os.chdir( path )# 修改当前工作目录
v2=os.system(commandtext1)

# 快速运行F5
# print(v2)

#关于python权限的问题
# https://blog.csdn.net/qq_33731081/article/details/103812749
# 如何在python中运行命令行命令
# https://blog.csdn.net/qq_34769162/article/details/119037908
#报错为空值的问题
# https://blog.csdn.net/xiaoxiaogh/article/details/88320102

#关于drawio导出命令,灵感来源https://j2r2b.github.io/2019/08/06/drawio-cli.html
'''
Options:
  -V, --version                      output the version number
  -c, --create                       creates a new empty file if no file is
                                     passed
  -k, --check                        does not overwrite existing files
  -x, --export                       export the input file/folder based on the
                                     given options
  -r, --recursive                    for a folder input, recursively convert
                                     all files in sub-folders also
  -o, --output <output file/folder>  specify the output file/folder. If
                                     omitted, the input file name is used for
                                     output with the specified format as
                                     extension
  -f, --format <format>              if output file name extension is
                                     specified, this option is ignored (file
                                     type is determined from output extension,
                                     possible export formats are pdf, png, jpg,
                                     svg, vsdx, and xml) (default: "pdf")
  -q, --quality <quality>            output image quality for JPEG (default:
                                     90)
  -t, --transparent                  set transparent background for PNG
  -e, --embed-diagram                includes a copy of the diagram (for PNG,
                                     SVG and PDF formats only)
  --embed-svg-images                 Embed Images in SVG file (for SVG format
                                     only)
  -b, --border <border>              sets the border width around the diagram
                                     (default: 0)
  -s, --scale <scale>                scales the diagram size
  --width <width>                    fits the generated image/pdf into the
                                     specified width, preserves aspect ratio.
  --height <height>                  fits the generated image/pdf into the
                                     specified height, preserves aspect ratio.
  --crop                             crops PDF to diagram size
  -a, --all-pages                    export all pages (for PDF format only)
  -p, --page-index <pageIndex>       selects a specific page, if not specified
                                     and the format is an image, the first page
                                     is selected
  -g, --page-range <from>..<to>      selects a page range (for PDF format only)
  -u, --uncompressed                 Uncompressed XML output (for XML format
                                     only)
  --enable-plugins                   Enable Plugins
  -h, --help                         display help for command
'''

读到这里,这篇“怎么使用draw.io插件在vscode中一体化导出高质量图片”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI