首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用SchemDraw库自动保存图像

用SchemDraw库自动保存图像
EN

Stack Overflow用户
提问于 2018-03-07 18:29:00
回答 1查看 1.1K关注 0票数 4

我想使用python中的这个库来生成电子图:https://cdelker.bitbucket.io/SchemDraw/,我想在服务器上运行这段代码。

这个想法是生成映像,将其保存在服务器中,然后通过url提供给客户端。

我使用它的示例代码来测试:

代码语言:javascript
复制
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环境变量中

我想这不可能在服务器上显示一个图像,因为它没有视觉界面。

有没有可能在不出示的情况下拯救这一切?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-26 15:28:22

官方文档现在有一个关于这个部分的部分: https://schemdraw.readthedocs.io/en/latest/usage/start.html#server-side

schemdraw 0.8开始,您可以直接将图像bytes保存到内存中的变量中,而不需要弹出文件或窗口。

代码语言:javascript
复制
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",看看它是否能解决这个问题:

代码语言:javascript
复制
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 FAQ后端是什么?

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49158923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档