Saves one .png image to the current directory, or a chosen folder.
save_image(data,
image_name=None,
colorm='inferno',
scalebar=False,
physical_size = (0, 'unit'),
colorbar = True,
size=None,
labelsize=16,
std_range=3,
saving_path='',
verbose=False,
show=False,
source_path=None,
source_scale_m_per_px=None)
INPUTS:
- data: A 2-D array which will be converted into a png image.
- image_name (default: None): name of the image that is saved. By default, tries to pull name from source_path. If this cannot be done, sets name to 'image'
- colorm (default: 'inferno'): colormap to be used for image
-
scalebar(default: False): if True, add a scalebar to the image, requires three attributes :
- shape, which define the pixel size of the image
- size, which gives the phyiscal dimension of the image
- unit, which give the physical unit of size
- physical_size (default: (0, 'unit')): physical size of the image used when generating the scalebar
- size (default: None): Dimension of the saved image. If none, the image is set to have one pixel per data point at 100 dpi
- labelsize (default: 16): Size of the text in pxs
- std_range (default: 3): Range around the mean for the colorscale, alternatively the value can be "full", to take the full range.
- saving_path (default: ''): The path to the folder where to save the image
- verbose (default: False): if True, print a line each time a image is saved.
- show (default: False): if True, the image is displayed in the kernel.
- source_path (default: None): if set, and image_name not set, this variable will be used to generate the file name
- source_scale_m_per_px (default: None): attempts to directly grab scale if attrs are provided
OUTPUTS:
- null
def save_image(data,
image_name=None,
colorm='inferno',
scalebar=False,
physical_size = (0, 'unit'),
colorbar = True,
size=None,
labelsize=16,
std_range=3,
saving_path='',
verbose=False,
show=False,
source_path=None,
source_scale_m_per_px=None):
if data.dtype == 'bool':
data = data.astype(int)
if image_name is None:
if source_path is not None:
image_name = source_path.replace('/','_')
else:
image_name = 'image'
if saving_path != '':
if saving_path[-1] != '/':
saving_path = saving_path +'/'
if std_range != 'full':
std_range = float(std_range)
if size is None:
figsize = (np.array(np.shape(data))/100)[::-1]
if figsize[0] < 5:
scale_factor = np.ceil(5/figsize[0])
figsize = scale_factor*figsize
fig = plt.figure(frameon=False, figsize=figsize, dpi=100)
else:
fig = plt.figure(figsize=size)
plt.tick_params(labelsize=labelsize)
offsetdata = data - np.nanmin(data)
mean_val = np.nanmean(offsetdata)
std_val = np.nanstd(offsetdata)
v_min = 0
v_max = mean_val + std_range*std_val
if std_range == 'full':
pos = plt.imshow(offsetdata, cmap=colorm)
else:
pos = plt.imshow(offsetdata, vmin=v_min, vmax=v_max, cmap=colorm)
if colorbar == True:
cbar = plt.colorbar(pos,fraction=0.046, pad=0.04)
cbar.ax.tick_params(labelsize=labelsize)
plt.tight_layout()
if scalebar:
try:
if source_scale_m_per_px is None:
phys_size = physical_size[0]
px = np.shape(data)[0]
scalebar = ScaleBar(phys_size/px, physical_size[1], location='lower right',
font_properties={'size':labelsize})
else:
scalebar = ScaleBar(source_scale_m_per_px, 'm', location='lower right',
font_properties={'size':labelsize})
#scalebar = ScaleBar(f[path].attrs['scale (m/px)'], 'm', location='lower right',
#font_properties={'size':25})
plt.gca().add_artist(scalebar)
except:
print("Error in the creation of the scalebar, please check that the attribute's\
size and shape are correctly define for each data channel.")
fig.savefig(saving_path+str(image_name)+'.png')
if show:
plt.show()
if verbose:
print(filename.split('.')[0]+'_'+str(image_name)+'.png saved.')
plt.close()
return