fzlakers
fzlakers

Reputation: 311

Uneven squares in grid image upon saving

So, I am trying to save a matplotlib figure as a TIFF. The image has all of the correct information and content but for some reason, after saving, the squares on the grid that I plot appear uneven. In the matplotlib image window that comes up after running the code though, the image has perfect squares. I have attached a code snippet and samples of the produced images below. They are screenshots of a much larger, 332x335 grid. The image generally looks okay but if it is to be used in scientific papers, as I intend, it should be as close to perfect as possible. If someone could help here, I would greatly appreciate it.

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()

Perfect squares from screenshot in plt.show() window: Perfect squares from screenshot in plt.show() window

Uneven squares when viewed after saving: Uneven squares when viewed after saving

Upvotes: 1

Views: 106

Answers (1)

fzlakers
fzlakers

Reputation: 311

I was actually able to resolve this. It turns out the more effective way of doing this is by using the PIL library. This also greatly reduced the overall file size.

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')

Upvotes: 1

Related Questions