Reputation: 389
import time
import picamera
import picamera.array
import numpy as np
with picamera.PiCamera() as camera:
camera.resolution = (100,100)
time.sleep(2)
image = np.empty((128,112, 3), dtype=np.uint8)
camera.capture(image, 'rgb')
image = image[:100, :100]
My question is - how do I get that 'image' saved as a .png file? I am hoping to capture images for a machine learning project.
The result of `help(camera.capture) is below:
capture(output, format=None, use_video_port=False, resize=None, splitter_port=0, bayer=False, **options) method of picamera.camera.PiCamera instance Capture an image from the camera, storing it in output.
If *output* is a string, it will be treated as a filename for a new
file which the image will be written to. If *output* is not a string,
but is an object with a ``write`` method, it is assumed to be a
file-like object and the image data is appended to it (the
implementation only assumes the object has a ``write`` method - no
other methods are required but ``flush`` will be called at the end of
capture if it is present). If *output* is not a string, and has no
``write`` method it is assumed to be a writeable object implementing
the buffer protocol. In this case, the image data will be written
directly to the underlying buffer (which must be large enough to accept
the image data).
It seems to me that the 'image' is going into an underlying buffer (which I know nothing about). How do I capture that buffer to a file? Am I thinking about this the wrong way?
Many thanks!
Upvotes: 1
Views: 1558
Reputation: 207748
You can use PIL, or other library to convert the Numpy array to a PIL Image
and save it...
from PIL import Image
im = Image.fromarray(YourNumpyImage)
im.save(’result.png’)
Upvotes: 2