Sitemap / Advertise

Information



Tags



Share

How to add image effects on the Raspberry Pi camera module in Python

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement




Definition

In this tutorial, I will show you how to add image effects to the Raspberry Pi camera module by creating a class in Python, which lets you define annotation text, framerate, resolution, and filename.

The image effect options are(1):

- none

- negative

- solarize

- sketch

- denoise

- emboss

- oilpaint

- hatch

- gpen

- pastel

- watercolor

- film

- blur

- saturation

- colorswap

- washedout

- posterise

- colorpoint

- colorbalance

- cartoon

- deinterlace1

- deinterlace2

Note: The default effect is none.

Code

- Include the required libraries.

- Create a class named camera_effects.

- Define the camera module settings - resolution and framerate - in the __init__() function.

- In the record() function, define the video file path and location and record a video with the given settings - filename, annotate, and effect.

- Define a new class object named as video.

- Enter the filename, annotation text, and select an image effect to apply from the list above.


-------------- Python --------------

from picamera import PiCamera
from time import sleep

class camera_effects:
    def __init__(self, r_x, r_y, framerate):
        # Define the camera module settings.
        self.camera = PiCamera()
        self.camera.resolution = (r_x, r_y)
        self.camera.framerate = framerate
    def record(self, filename, annotate, effect):
        # Define video file path and location.
        path = '/home/pi/' + filename
        # Record a video with the given settings.
        self.camera.annotate_text = annotate
        self.camera.image_effect = effect
        self.camera.start_preview()
        self.camera.start_recording()
        sleep(30)
        self.camera.stop_recording()
        self.camera.stop_preview()
        print("Rasp_Pi => Video Recorded! \r\n")

# Define a new class object named as 'video'.
video = camera_effects(800, 600, 30)
                
#Record a video with the selected camera effect:
#"none"
#"negative"
#"solarize"
#"sketch"
#"pastel"
#"watercolor"
#"film"
#"blur"
#"saturation"
#"posterise"
#"cartoon"
#"colorpoint"
#"colorbalance"
# ...

video.record('test.h264', 'Testing Effects', 'negative')

Result:

You can inspect my electronics project in which the mentioned method is used to develop a video recoder and uploader GUI allowing the user to implement image effects on the interface from here.

References

(1) https://projects.raspberrypi.org/en/projects/getting-started-with-picamera/7