我想在Python中使用这个库来生成电气图:https://cdelker.bitbucket.io/SchemDraw/ https://cdelker.bitbucket.io/SchemDraw/,
我想在服务器中运行这段代码。
这个想法是生成图像,将其保存在服务器中,然后通过 url 将其提供给客户端。
我正在使用它的示例代码来测试:
import SchemDraw as schem
import SchemDraw.elements as e
d = schem.Drawing()
V1 = d.add(e.SOURCE_V, label='10V')
d.add(e.RES, d='right', label='100K$\Omega$')
d.add(e.CAP, d='down', botlabel='0.1$\mu$F')
d.add(e.LINE, to=V1.start)
d.add(e.GND)
d.draw()
d.save('testschematic.svg')
它工作正常,但问题是图像出现,我需要手动保存它,如果我在服务器中执行此代码,它会给我错误:
文件“/usr/local/lib/python3.5/tkinter/init.py”,第 1877 行,在initapi_1 | self.tk = _tkinter.create(屏幕名称,基本名称,类名称,交互式,wantobjects,useTk,同步,使用)api_1
| _tkinter.TclError:没有显示名称并且没有 $DISPLAY 环境
多变的
我想在服务器中显示图像是不可能的,因为它没有可视化界面。
有没有可能在不显示的情况下保存它?
官方文档现在有一个章节:https://schemdraw.readthedocs.io/en/latest/usage/start.html#server-side https://schemdraw.readthedocs.io/en/latest/usage/start.html#server-side
As of schemdraw
0.8,可以保存图像bytes
直接访问内存中的变量,无需弹出文件或窗口。
from schemdraw import Drawing, ImageFormat
import matplotlib
matplotlib.use('Agg') # Set the backend here
drawing = Drawing()
# Add circuit components here.
# Save the schematic bytes to a variable.
image_bytes = drawing.get_imagedata(ImageFormat.SVG)
For schemdraw <= 0.7.1
:
SchemDraw 作者 Collin Delker 在以下位置提供了此建议:https://www.collindelker.com/2014/08/29/electrical-schematic-drawing-python.html https://www.collindelker.com/2014/08/29/electrical-schematic-drawing-python.html使用以下命令更改 matplotlib 后端matplotlib.use https://matplotlib.org/api/matplotlib_configuration_api.html.
SchemDraw 在 Matplotlib 图形上绘制,因此如果您有交互式 Matplotlib 后端,它将尝试在窗口中显示图像,这在服务器上是不可能的。首先尝试将后端设置为其他内容,例如“Agg”,然后看看这是否可以解决问题:
import matplotlib
matplotlib.use('Agg') # Set the backend here
import SchemDraw as schem
import SchemDraw.elements as e
d = schem.Drawing()
...
d.draw()
d.save('mycircuit.svg')
这对我有用。
也可以看看:matplotlib 常见问题解答 什么是后端? https://matplotlib.org/faq/usage_faq.html#what-is-a-backend
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)