matplotlib 在经过一些迭代后"fail to allocate bitmap"



我找不到避免此崩溃的解决方案。

我已经清理了附加的代码,因为它包含问题,而没有其他内容。

不管图像的大小如何,程序在368次迭代后都会崩溃。

我也尝试了我在论坛上能找到的东西,但没有找到解决方案(plt.close('all'(,gc。collect((…(。

import matplotlib.pyplot as plt
import cv2

compteur = 0
image = cv2.imread(r"D:OneDriveBureauNew folder12345.jpg")

while True:
print('1')
ax_user = plt.imshow(image)
print('2')
plt.close('all')
print (f'n{compteur}:t')
compteur += 1 

367:
1
2
368:
1
Fail to allocate bitmap

根据这篇文章,这是TkAgg后端的一个问题。

我能够使用使其工作

reload(matplotlib)
matplotlib.use('Agg')

我得再打一次,看看这是否稳健。

我在这个代码中看到了错误消息:

http://search.cpan.org/src/NI-S/Tk-804.027/pTk/mTk/win/tkWinDraw.c

if(!bitmap) {
panic("Fail to allocate bitmapn");
DeleteDC(dcMem);
TkWinReleaseDrawableDC(d, dc, &state);
return;
}

我不清楚为什么C源代码中的bitmap为空,但我注意到Python内存的使用量随着后续创建另一个绘图/图像/图形的调用而不断增加,无论是否使用plt.close('all')plt.clf()gc.collect()等,所以它的创建失败可能与此有关。我没有注意到同样的行为,例如,阿格的后端。

可以在文档中阅读更多关于后端选项的信息,或者查看文档中的matplotlib.use():

use(backend, *, force=True)
Select the backend used for rendering and GUI integration.

Parameters
----------
backend : str
The backend to switch to.  This can either be one of the standard
backend names, which are case-insensitive:

- interactive backends:
GTK3Agg, GTK3Cairo, MacOSX, nbAgg,
Qt4Agg, Qt4Cairo, Qt5Agg, Qt5Cairo,
TkAgg, TkCairo, WebAgg, WX, WXAgg, WXCairo

- non-interactive backends:
agg, cairo, pdf, pgf, ps, svg, template

or a string of the form: ``module://my.module.name``.

force : bool, default: True
If True (the default), raise an `ImportError` if the backend cannot be
set up (either because it fails to import, or because an incompatible
GUI interactive framework is already running); if False, ignore the
failure.

See Also
--------
:ref:`backends`
matplotlib.get_backend

最新更新