本文介绍了将 matplotlib 图保存到 tiff 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用 matplotlib 以 .png 格式成功保存绘图.但是,当我尝试将其保存为 .tiff 格式时,出现此错误:
I can successfully save a plot using matplotlib in .png format. However, when I try to save it in .tiff format, I get this error:
plt.savefig(base_dir+'Plots'+os.sep+path+os.sep+title+'.tiff', dpi=500)
错误:
File "C:/Users/r/workspace/FLE/plots.py", line 1310, in plot_by_region
plt.savefig(base_dir+'Plots'+os.sep+path+os.sep+title+'.tiff', dpi=500)
File "C:\Anaconda64\lib\site-packages\matplotlib\pyplot.py", line 688, in savefig
res = fig.savefig(*args, **kwargs)
File "C:\Anaconda64\lib\site-packages\matplotlib\figure.py", line 1539, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Anaconda64\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 196, in print_figure
FigureCanvasAgg.print_figure(self, *args, **kwargs)
File "C:\Anaconda64\lib\site-packages\matplotlib\backend_bases.py", line 2230, in print_figure
**kwargs)
File "C:\Anaconda64\lib\site-packages\matplotlib\backends\backend_agg.py", line 594, in print_tif
dpi=dpi)
File "C:\Anaconda64\lib\site-packages\PIL\Image.py", line 1665, in save
save_handler(self, fp, filename)
File "C:\Anaconda64\lib\site-packages\PIL\TiffImagePlugin.py", line 1320, in _save
offset = ifd.save(fp)
File "C:\Anaconda64\lib\site-packages\PIL\TiffImagePlugin.py", line 608, in save
if len(data) <= 4:
TypeError: object of type 'int' has no len()
推荐答案
回滚到 Pillow 2.9.0 并保存到 .tiff 有效.请参阅此处:https://github.com/python-pillow/Pillow/issues/1524
rolling back to Pillow 2.9.0 and saving to .tiff works. See here: https://github.com/python-pillow/Pillow/issues/1524
这篇关于将 matplotlib 图保存到 tiff 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!