保存时网格图像中的正方形不均匀

时间:2019-11-18 19:01:59

标签: python matplotlib

因此,我正在尝试将matplotlib图形保存为TIFF。该图像具有所有正确的信息和内容,但是由于某种原因,保存后,我绘制的网格上的正方形显得不均匀。但是,在运行代码后出现的matplotlib图像窗口中,图像具有完美的正方形。我在下面附上了一个代码片段和所生成图像的样本。它们是更大的332x335网格的屏幕截图。图像通常看起来还不错,但是如果按照我的意图将其用于科学论文,则应尽可能接近完美。如果有人可以在这里提供帮助,我将不胜感激。

fname = tif_file_name+'.tif'
aspect = grid_x/grid_y
plt.figure()
plt.imshow(circ_avg, cmap='gray', aspect=aspect, interpolation='none',)
plt.gca().invert_yaxis()
plt.savefig(fname, dpi = 1000, format='tif', bbox_inches='tight', pad_inches = 0)
plt.show()

来自plt.show()窗口中屏幕截图的完美正方形: Perfect squares from screenshot in plt.show() window

保存后查看时不均匀的正方形: Uneven squares when viewed after saving

1 个答案:

答案 0 :(得分:1)

我实际上能够解决此问题。事实证明,更有效的方法是使用PIL库。这也大大减小了文件的总大小。

from PIL import Image

#scale to pixel vals (only multiplied by 255 here since my data already had 1 as the maximum)
vals= orig_vals*255
final_image = Image.fromarray(np.uint8(vals), mode='L')
final_image.save('blah.tif')