Capture images in different formats with Raspberry Pi camera module

0
4374
Raspberry Pi & Camera
Raspberry Pi & Camera

In previous article, I’ve shown you how to capture Pi camera images, and let’s try to capture in different formats.

The camera.capture() method is used for capturing images with,

  • the first parameter: to specify the file name to saved.
  • the second parameter: to specify the file format to be used. The default format value is None, and picamera will try to guess the format based on file name, if it fails, then it will raise an Error.

The following script will try to capture camera images in common formats.


# app.py
import picamera
formats = [ 'jpeg', 'png', 'gif', 'bmp' ]
camera = picamera.PiCamera()
camera.start_preview()
for f in formats:
    print('Capturing format: ' + f)
    camera.capture('snapshot.' + f, format = f)
camera.stop_preview()

According to the documentation, the following formats are supported:

jpeg‘ – Write a JPEG file
png‘ – Write a PNG file
gif‘ – Write a GIF file
bmp‘ – Write a Windows bitmap file
yuv‘ – Write the raw image data to a file in YUV420 format
rgb‘ – Write the raw image data to a file in 24-bit RGB format
rgba‘ – Write the raw image data to a file in 32-bit RGBA format
bgr‘ – Write the raw image data to a file in 24-bit BGR format
bgra‘ – Write the raw image data to a file in 32-bit BGRA format
raw‘ – Deprecated option for raw captures; the format is taken from the deprecated raw_format attribute

Have fun toying with camera 🙂