Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scipy: savefig without frames, axes, only content

In numpy/scipy I have an image stored in an array. I can display it, I want to save it using savefig without any borders, axes, labels, titles,... Just pure image, nothing else.

I want to avoid packages like PyPNG or scipy.misc.imsave, they are sometimes problematic (they do not always install well, only basic savefig() for me

like image 310
Jakub M. Avatar asked Nov 21 '11 21:11

Jakub M.


People also ask

How do I get rid of white spaces in Matplotlib?

MatPlotLib with Python To remove whitespaces at the bottom of a Matplotlib graph, we can use tight layout or autoscale_on=False.

How do I save a Matplotlib plot without white space?

Hide the Whitespaces and Borders in Matplotlib Figure To get rid of whitespace around the border, we can set bbox_inches='tight' in the savefig() method. Similarly, to remove the white border around the image while we set pad_inches = 0 in the savefig() method.


2 Answers

EDIT

Changed aspect='normal to aspect='auto' since that changed in more recent versions of matplotlib (thanks to @Luke19).


Assuming :

import matplotlib.pyplot as plt 

To make a figure without the frame :

fig = plt.figure(frameon=False) fig.set_size_inches(w,h) 

To make the content fill the whole figure

ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) 

Then draw your image on it :

ax.imshow(your_image, aspect='auto') fig.savefig(fname, dpi) 

The aspect parameter changes the pixel size to make sure they fill the figure size specified in fig.set_size_inches(…). To get a feel of how to play with this sort of things, read through matplotlib's documentation, particularly on the subject of Axes, Axis and Artist.

like image 90
matehat Avatar answered Oct 09 '22 18:10

matehat


An easier solution seems to be:

fig.savefig('out.png', bbox_inches='tight', pad_inches=0) 
like image 39
weatherfrog Avatar answered Oct 09 '22 19:10

weatherfrog