Linux Format

Pi camera effects..................

Les Pounder makes a great attempt to persuade us that selfies are artistic, but in the process shows us how to use the Pi to create selfies with a twist.

-

Les Pounder takes us on an artistic coding journey to generate selfies with a twist.

The Raspberry Pi Foundation recently released a new version of its popular camera attachment. In this tutorial we’ll use the camera to create a special effects selfie machine. This project will also work with the older Raspberry Pi Camera and all versions of the Raspberry Pi with a camera port. As well as the official Pi Camera, you need a breadboard, a momentary switch and a female to male jumper wire. All of the code for this project and a detailed diagram of the circuit can be downloaded from http://bit.ly/PiSelfieCa­mera.

To install the Pi Camera, first, we need to ensure that our Raspberry Pi is turned off and locate the camera port, which is between the HDMI and Ethernet port. The port has a pull tab, which you’ll need to pull upwards until it can go no further. Now insert the ribbon cable of the camera into the port, with the blue side facing the Ethernet port. The ribbon will only go so far and when ready push the port tab back into place to secure the ribbon cable. While our Raspberry Pi is off, let’s connect the button that will be used to trigger our camera. Please refer to the diagram ( seebottomp­61) for instructio­ns on how to wire up your button. Next, attach your keyboard, mouse, HDMI and any other accessorie­s to your Pi, and power up and log into the desktop. We now need to tell our Raspberry Pi that we have connected the camera. In the main menu, located in the top-left of the screen, go to the Preference­s menu and select ‘Raspberry Pi Configurat­ion’ applicatio­n. In the applicatio­n click on ‘Interfaces’ and enable the Camera. To effect the change you’ll need to reboot your Raspberry Pi and you will be prompted to do so.

Once your Raspberry Pi has rebooted and returned to the desktop, open a terminal, you can see the icon for this in the top left of the screen. We are going to use a terminal command to test that our camera is working correctly. In the terminal type $ raspistill -o test.jpg . You will see a preview window appear, frame your picture and in five seconds the test will be completed and will save an image to your home directory. If any errors are encountere­d then repeat the previous steps.

Selfie machine

We now start our project, our goal is to create a selfie machine that will produce a random artistic effect to the photograph. We will be using Python 3 editor, found in the programmin­g menu. When the Python 3 editor window opens, click on File > New to open a new blank window. Immediatel­y save your work by clicking on File > Save and call the project selfie_camera.py and you’ll find that subsequent saves will be quicker.

Our first section of code contains a series of imports, which enable our project to work with Python modules and add access to more functional­ity: from time import sleep from random import choice from picamera import PiCamera from gpiozero import Button

We import the sleep function from the time module, this enables our project speed to be controlled. Next, we import the choice function from the random module, using choice we can randomly choose an item from a list. Our third import is the picamera module, which we rename to PiCamera . Last, we import the Button class from gpizero .

With our imports complete we now go on to create two variables: camera = PiCamera() button = Button(17)

Our first variable, camera , is used to shorten the module reference for PiCamera() . Our second variable is button and we use that to store a reference to the GPIO pin that is used for our momentary switch. Our next line of code will start a live preview from the camera. In a similar fashion to seeing a preview shot on our smartphone: camera.start_preview(alpha=192)

You will see that we use the argument alpha = 192 as this enables a transparen­t effect on the preview, just in case we have an error and so we can see the desktop.

Our next line of, very simple, code is a variable called i: i=1 , which will store an integer, in this case 1 . We will use i to count the number of photos that are being taken.

We now move on and create a list that will store the special effects options available for the camera. There are quite a few of these and in the code snippet we will only show a few, but the full list is available in the download for this project.

We’ve reached the main section of code for our project, which controls how our selfie machine works. All of the code is enclosed in an infinite loop: while True: . Our first line of code inside the loop is designed to wait for the user to press the button/momentary switch attached to pin 17: button.wait_for_press() You’ll note that we’ve used the variable button to store this location earlier.

Once the button has been pressed our code will create a new variable called effect and in there we will store a randomly chosen special effect from our list of effects that we created earlier. Each time the loop runs it will choose a different effect, but as the list is quite small, there will be duplicatio­n: effect = choice(effects) .

With the special effect chosen, we now apply that choice and instruct the camera to use that effect: camera.image_

effect = effect . To confirm that we have chosen a new effect we print the name of the effect to the Python shell with:

print('This effect is’,effect) . Next, we instruct the camera to capture an image and save the image to the desktop. camera.capture('/home/pi/Desktop/image%d.jpg’ % i)

You will see that we give each photo taken an incremente­d filename. We do this using the i variable that we earlier created. We use a substation template, in this case %d , which is a decimal/whole number (1,2,3 etc), we instruct the template to substitute the d for the value of our variable i .

We now increment the value of i so that it progresses to the next number in the sequence with: i=i+1 . Last, we come out of the infinite loop and ensure that the camera preview window closes if the loop is broken using: camera.

stop_preview() . With the code completed, ensure that it has been saved. From the Python editor window click on Run > Run Module to run the project. Get ready to smile and take a lot of funny photos. LXF

 ??  ?? Inserting the camera cable into the port can be a little tricky.
Inserting the camera cable into the port can be a little tricky.
 ??  ?? Our project will produce weird images just like this, yes this is a picture of Les taken using the gpen effect.
Our project will produce weird images just like this, yes this is a picture of Les taken using the gpen effect.
 ??  ?? It uses an extremely simple circuit, with just a button, a breadboard and two wires from our Pi.
It uses an extremely simple circuit, with just a button, a breadboard and two wires from our Pi.
 ??  ??

Newspapers in English

Newspapers from Australia