Examples Codes

Hello from plugin

pibooth_hello.py

"""Plugin to log a message when entering in the 'wait' state."""

import pibooth
from pibooth.utils import LOGGER

__version__ = "1.0.0"

@pibooth.hookimpl
def state_wait_enter():
    LOGGER.info("Hello from '%s' plugin", __name__)

Upload to FTP

pibooth_ftp.py

"""Plugin to upload pictures on a FTP server."""

import os
from ftplib import FTP
import pibooth

__version__ = "0.0.2"

@pibooth.hookimpl
def pibooth_startup(app):
    app.ftp = FTP()
    app.ftp.set_debuglevel(0)
    app.ftp.connect("ftp.pibooth.org", 21)
    app.ftp.login("pibooth", "1h!gR4/opK")

@pibooth.hookimpl
def state_processing_exit(app):
    name = os.path.basename(app.previous_picture_file)

    with open(app.previous_picture_file, 'rb') as fp:
        app.ftp.storbinary('STOR {}'.format(name), fp, 1024)

@pibooth.hookimpl
def pibooth_cleanup(app):
    app.ftp.close()

Control a RGB LED

pibooth_rgb_led.py

"""Plugin to manage the RGB lights via GPIO."""

import pibooth
from gpiozero import RGBLED
from colorzero import Color

__version__ = "1.1.0"

@pibooth.hookimpl
def pibooth_startup(app):
    # GPIOZERO is configured as BCM, use string with "BOARD(pin)" to
    # convert on BOARD
    app.rgbled = RGBLED("BOARD36", "BOARD38", "BOARD40")

@pibooth.hookimpl
def state_wait_enter(app):
    app.rgbled.color = Color('green')

@pibooth.hookimpl
def state_choose_enter(app):
    app.rgbled.blink()

@pibooth.hookimpl
def state_preview_enter(app):
    app.rgbled.color = Color('white')
    app.rgbled.blink()

@pibooth.hookimpl
def state_capture_exit(app):
    app.rgbled.color = Color('red')

Add ‘Get Ready’ text before captures sequence

pibooth_getready_text.py

"""Plugin to display 'Get Ready' at screen after 'wait' state."""

import time
import pygame
import pibooth
from pibooth import pictures, fonts

__version__ = "0.0.2"


@pibooth.hookimpl
def state_wait_exit(win):
    win_rect = win.get_rect()
    text = "Get Ready!"

    # Get best font size according to window size
    font = fonts.get_pygame_font(text, fonts.CURRENT,
                                 win_rect.width//1.5, win_rect.height//1.5)

    # Build a surface to display at screen
    text_surface = font.render(text, True, win.text_color)

    # Clear screen
    if isinstance(win.bg_color, (tuple, list)):
        win.surface.fill(win.bg_color)
    else:
        bg_surface = pictures.get_pygame_image(win.bg_color, win_rect.size, crop=True, color=None)
        win.surface.blit(bg_surface, (0, 0))

    # Draw the surface at screen
    win.surface.blit(text_surface, text_surface.get_rect(center=win_rect.center).topleft)

    # Force screen update and events process
    pygame.display.update()
    pygame.event.pump()

    # Wait 1s
    time.sleep(1)

Setup a custom camera

pibooth_custom_camera.py

"""Plugin to handle retry in case of exception with DSLR/gPhoto2 camera."""

import time
import pibooth
from pibooth.utils import LOGGER
from pibooth import camera

__version__ = "4.0.2"


class GpCameraRetry(camera.GpCamera):

    def capture(self, effect=None):
        """Capture a new picture.
        """
        retry = 0
        max_retry = 2
        while retry < max_retry:
            try:
                return super(GpCameraRetry, self).capture(effect)
            except Exception:
                LOGGER.warning("Gphoto2 fails to capture, trying again...")
            retry += 1
        raise EnvironmentError("Gphoto2 fails to capture {} times".format(max_retry))


class HybridRpiCameraRetry(camera.HybridRpiCamera):

    def __init__(self, rpi_camera_proxy, gp_camera_proxy):
        super(HybridRpiCamera, self).__init__(rpi_camera_proxy)
        self._gp_cam = GpCameraRetry(gp_camera_proxy)
        self._gp_cam._captures = self._captures  # Same dict for both cameras


@pibooth.hookimpl
def pibooth_setup_camera(cfg):
    rpi_cam_proxy = camera.get_rpi_camera_proxy()
    gp_cam_proxy = camera.get_gp_camera_proxy()

    if rpi_cam_proxy and gp_cam_proxy:
        LOGGER.info("Configuring hybrid camera with retry (Picamera + gPhoto2) ...")
        return HybridRpiCameraRetry(rpi_cam_proxy, gp_cam_proxy)
    elif gp_cam_proxy:
        LOGGER.info("Configuring gPhoto2 camera with retry ...")
        return GpCameraRetry(gp_cam_proxy)
    elif rpi_cam_proxy:
        LOGGER.info("Configuring Picamera camera ...")
        return camera.RpiCamera(rpi_cam_proxy)

Setup a custom picture factory

pibooth_custom_factory.py

"""Pibooth plugin which return the first raw capture as final picture."""

import pibooth
from pibooth.pictures.factory import PictureFactory


__version__ = "1.0.0"


class IdlePictureFactory(PictureFactory):

    def build(self, rebuild=False):
        return self._images[0]


@pibooth.hookimpl
def pibooth_setup_picture_factory(factory):
    return IdlePictureFactory(factory.width, factory.height, *factory._images)