Add Special Effects Using Python

The PiCamera software provides a number of effects and other configurations you can apply. Write some Python code to apply these effects.

Step 1

You can change the resolution of the camera. 

By default, it's set to the resolution of your monitor, but the maximum resolution is 2592 x 1944 for still photos and 1920 x 1080 for video recording. 

Type code to set the resolution to max. Set the frame rate to 15 to enable this maximum resolution:

camera.resolution = (2592, 1944) camera.framerate = 15 camera.start_preview() sleep(5) camera.capture('/home/pi/Desktop/max.jpg') camera.stop_preview()

Step 2

The minimum resolution is 64 x 64. Try taking a video at that resolution and open it to see the difference.

Step 3

Add text to your image with annotate_text:

camera.start_preview() camera.annotate_text = "Hello world!" sleep(5) camera.capture('/home/pi/Desktop/text.jpg') camera.stop_preview()

Step 4

Adjust the brightness in a loop, and display the current brightness level:

camera.start_preview() camera.brightness = 70 sleep(5) camera.capture('/home/pi/Desktop/bright.jpg') camera.stop_preview()

Step 5

Try the same for the contrast:

camera.start_preview() for i in range(100): camera.annotate_text = "Contrast: %s" % i camera.contrast = i sleep(0.1) camera.stop_preview()

Step 6

Set the text size using this code:

camera.annotate_text_size = 50

The default size is 32. You can choose any number between 6 and 160.

Step 7

Alter the annotation colours. Ensure that 'Color' is imported by amending your import line at the top. Then amend the rest of your code as follows:

from picamera import PiCamera, Color from time import sleep camera = PiCamera() camera.start_preview() camera.annotate_background = Color('blue') camera.annotate_foreground = Color('yellow') camera.annotate_text = " Hello world " sleep(5) camera.stop_preview()

Step 8

Use camera.image_effect to apply a particular image effect. 

The options are:

  • none
  • negative
  • solarize
  • sketch
  • denoise
  • emboss
  • oilpaint
  • hatch
  • gpen
  • pastel
  • watercolor
  • film
  • blur
  • saturation
  • colorswap
  • washedout
  • posterise
  • colorpoint
  • colorbalance
  • cartoon
  • deinterlace1
  • deinterlace2

The default is none. 

Pick one and try it out:

camera.start_preview() camera.image_effect = 'colorswap' sleep(5) camera.capture('/home/Desktop/colorswap.jpg') camera.stop_preview()

Step 9

Loop over the various image effects in a preview to test them out:

camera.start_preview() for effect in camera.IMAGE_EFFECTS: camera.image_effect = effect camera.annotate_text = "Effect: %s" % effect sleep(5) camera.stop_preview()

Step 10

Use camera.awb_mode to set the auto white balance to a pre-set mode to apply a particular effect. 

The options are: 

  • off
  • auto
  • sunlight
  • cloudy
  • shade
  • tungsten
  • fluorescent
  • incandescent
  • flash
  • horizon

The default is auto. 

Pick one and try it out:

camera.start_preview() camera.awb_mode = 'sunlight' sleep(5) camera.capture('/home/pi/Desktop/sunlight.jpg') camera.stop_preview()

Loop over the available auto white balance modes with camera.AWB_MODES.

Step 11

Use camera.exposure_mode to set the exposure to a pre-set mode to apply a particular effect. 

The options are:

  • off
  • auto
  • night
  • nightpreview
  • backlight
  • spotlight
  • sports
  • snow
  • beach
  • verylong
  • fixedfps
  • antishake
  • fireworks

The default is auto. 

Pick one and try it out:

camera.start_preview() camera.exposure_mode = 'beach' sleep(5) camera.capture('/home/pi/Desktop/beach.jpg') camera.stop_preview()

Loop over the available exposure modes with camera.EXPOSURE_MODES.

Step 12

To learn more ways to use PiCamera, read the online documentation: https://picamera.readthedocs.io/