Save Images

Goal

This post aims to introduce how to save images using matplotlib.

Reference

Libraries

In [2]:
import numpy as np
import matplotlib.pyplot as plt

Create a image to save

In [6]:
img = np.random.randint(0, 255, (64, 64))
img
Out[6]:
array([[216, 200, 219, ...,  82, 176, 244],
       [ 17,  90,  86, ...,  91, 234, 195],
       [ 34, 226, 103, ..., 230,  86, 127],
       ...,
       [191, 110,  33, ...,  62, 109,  26],
       [ 43, 238, 208, ...,  51,   0, 123],
       [163, 156, 235, ..., 212, 188,  25]])

Show an image

In [9]:
plt.imshow(img);
plt.axis('off');

Save an image as png

In [33]:
plt.savefig('../images/savefig_sample_image.png')
<Figure size 432x288 with 0 Axes>
In [36]:
!ls ../images | grep image.png
savefig_sample_image.png

Comments

Comments powered by Disqus