Python绘图图像保存错误

7

我是Python的新手。我使用Django编写Web服务来保存服务器中的图像。我正在尝试使用以下代码将绘图保存为图像,前三次都可以正常工作,但在此之后,Python会抛出错误。如何解决这个问题?

Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x000000000FE40048>>
Traceback (most recent call last):
  File "c:\dev\lib\tkinter\__init__.py", line 3504, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x0000000010996438>>
Traceback (most recent call last):
  File "c:\dev\lib\tkinter\__init__.py", line 3504, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x0000000010B8BF98>>
Traceback (most recent call last):
  File "c:\dev\lib\tkinter\__init__.py", line 3504, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread


**PYTHON CODE**  
fileName = id_generator()+".png"
filePath = settings.MEDIA_ROOT+"/"+fileName
fig.savefig(filePath, dpi=fig.dpi)
plt.close(fig) 
Response(fileName, status=status.HTTP_201_CREATED)

Package             Version
------------------- ---------
cassandra-driver    3.14.0
certifi             2018.4.16
cycler              0.10.0
Django              2.0.5
djangorestframework 3.8.2
kiwisolver          1.0.1
matplotlib          2.2.2
numpy               1.14.4
pandas              0.23.0
pip                 10.0.1
pyparsing           2.2.0
python-dateutil     2.7.3
pytz                2018.4
scikit-learn        0.19.1
scipy               1.1.0
setuptools          39.2.0
six                 1.11.0
wheel               0.31.1
wincertstore        0.2
1个回答

11

我在不同的情况下遇到了相同的问题。如答案中所提到的,应该添加matplotlib.use('Agg')。这应该可以解决你的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接