gpt4 book ai didi

python - "error reading PNG header"运行时错误

转载 作者:行者123 更新时间:2023-12-01 05:08:58 28 4
gpt4 key购买 nike

每隔一段时间,我尝试使用 matplotlibcanvas.print_figure 绘制的图就会失败,并出现我无法理解的 RuntimeError 。对我来说提供一个最小的运行示例会非常复杂(特别是因为我无法真正重现它),但我可以将调用和回溯放在这里,我希望有人能够指出我应该看的地方为了追踪问题。

这是函数调用:

canvas.print_figure(filename, papertype='a4', orientation='landscape',
dpi=300, format='png')

其中canvas是一个FigureCanvas实例,filename类似于picture1110101.png

这是完整的回溯(我用点替换了长路径名):

Traceback (most recent call last):
File ".../draw.py", line 235, in plot
_plot(*args[1:], **kwargs)
File ".../draw.py", line 177, in _plot
dpi=dpi, format=ftype)
File ".../matplotlib/backend_bases.py", line 2093, in print_figure
**kwargs)
File ".../matplotlib/backends/backend_agg.py", line 491, in print_png
FigureCanvasAgg.draw(self)
File ".../matplotlib/backends/backend_agg.py", line 439, in draw
self.figure.draw(self.renderer)
File ".../matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File ".../matplotlib/figure.py", line 999, in draw
func(*args)
File ".../matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File ".../matplotlib/axes.py", line 2086, in draw
a.draw(renderer)
File ".../matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File ".../matplotlib/axis.py", line 1053, in draw
tick.draw(renderer)
File ".../matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File ".../matplotlib/axis.py", line 240, in draw
self.label1.draw(renderer)
File ".../matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File ".../matplotlib/text.py", line 576, in draw
self._fontproperties, angle)
File ".../matplotlib/backends/backend_agg.py", line 226, in draw_tex
Z = texmanager.get_grey(s, size, self.dpi)
File ".../matplotlib/texmanager.py", line 510, in get_grey
X = read_png(os.path.join(self.texcache, pngfile))
RuntimeError: _image_module::readpng: error reading PNG header

所以它看起来像是与 TeX 相关的东西。我确实使用 TeX,并且在包初始化文件的开头有以下配置行:

matplotlib.rc("text", usetex=True)

有什么提示吗?

最佳答案

这是当 latex -> dvi -> png 转换的 png 输出最终成为一个空但存在的文件时引起的。当 mpl 尝试读取 png 将其添加到 Canvas 并失败时,会引发错误,因为它不是有效的 png(由于为空)。

作为背景,mpl 通过 Agg 后端将 latex 插入到您的图形中的方式是 (iirc):

  1. 将 latex 字符串插入模板
  2. 对 Latex 进行系统调用以渲染模板 -> dvi
  3. 将 dvi -> png 转换为 dvipng
  4. 读回 png 并将其作为图像插入到您的图形中

我会捕获异常并重试。

关于python - "error reading PNG header"运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24548446/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com