我想使用python中的这个库来生成电子图: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行,在init api_1 self.tk = _tkinter.create(screenName、baseName、className、交互式、万托宾语、useTk、同步、使用) api_1 _tkinter.TclError:无显示名和无$DISPLAY环境变量中
我想这不可能在服务器上显示一个图像,因为它没有视觉界面。
有没有可能在不出示的情况下拯救这一切?
发布于 2018-06-26 15:28:22
官方文档现在有一个关于这个部分的部分: https://schemdraw.readthedocs.io/en/latest/usage/start.html#server-side
从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)对于schemdraw <= 0.7.1
SchemDraw作者Collin在https://www.collindelker.com/2014/08/29/electrical-schematic-drawing-python.html上提供了使用matplotlib.use更改matplotlib后端的建议。
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')对我起作用了。
https://stackoverflow.com/questions/49158923
复制相似问题