我找不到避免这种崩溃的解决方案。
我已经清理了附加的代码,因为它包含问题而没有其他内容。
无论图像大小如何,程序在 368 次迭代后就会崩溃。
我也尝试了在论坛中找到的内容,但没有找到解决方案( plt.close('all'), gc.collect().... )。
import matplotlib.pyplot as plt
import cv2
compteur = 0
image = cv2.imread(r"D:\OneDrive\Bureau\New folder\12345.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
Run Code Online (Sandbox Code Playgroud)
根据这篇文章,这是 TkAgg 后端的问题。
我能够使用以下方法让它工作:
Run Code Online (Sandbox Code Playgroud)reload(matplotlib) matplotlib.use('Agg')
我还得再试几次,看看这是否可靠。
我在这段代码中看到了错误消息:
http://search.cpan.org/src/NI-S/Tk-804.027/pTk/mTk/win/tkWinDraw.c
Run Code Online (Sandbox Code Playgroud)if(!bitmap) { panic("Fail to allocate bitmap\n"); DeleteDC(dcMem); TkWinReleaseDrawableDC(d, dc, &state); return; }
我不清楚为什么bitmap
C 源代码中的 为空,但我注意到 Python 内存使用量随着后续调用创建另一个绘图/图像/图形而不断增加,无论是否使用plt.close('all')
、plt.clf()
、gc.collect()
等,所以它的失败可能与此有关。例如,我没有注意到 Agg 后端有相同的行为。
人们可以在文档中阅读有关后端选项的更多信息,或检查文档matplotlib.use()
:
Run Code Online (Sandbox Code Playgroud)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
归档时间: |
|
查看次数: |
2361 次 |
最近记录: |