linux mint IT admin tips info

First Python Scripts For Pi Camera

I'm not a programmer at all - never was - I tried a few times at college with C and Java, but hated it...

So this is an enjoyable start for me, as there is immediate satisfaction, as Python is "English"; plain and simple as it gets with programming - so far anyway - one script only into it.

But at least it works immediately, with just python3, some libs and tools installed and the vim editor to write/paste with!

All I have done with the PiCam is enable it and create a video, as you saw in the prior posts using:


raspivid -o /vidname.h264

You can change the view time of that to a ten second long recording:

raspivid -t 10000 -o /videoname.h264

Your h264 video is viewable in VLC for Windows/Mint, or in the Pi using the same method for the "hello..." range of examples in the /opt/vc/src/hello_pi/hello_video folders, using the latent binary prefix:

root@pi:/opt/vc/src/hello_pi/hello_video# ./hello_video.bin test.h264

So, how to get the camera going to make vids and pics in Python, learn functionality and get up to a point where video can be streamed for a surveillance camera?

Research weblinks of course, but it would be good to get some background tech understanding too eh?

The initial hard part is finding out what you may need to get the camera setup optimally at first with firmware updates etc. so it is compatible with whatever environment you decide to use, as there are options.

I found these introductory pages useful to get started as a background and first scripts sources:

First, I upgraded the firmware for the Pi from the first link - important I feel, to have a reference for where you start, with what capabilities for debug later:

You can determine the revision of your current firmware with the following command:

$ uname -a

The firmware revision is the number after the #:

Linux kermit 3.12.26+ #707 PREEMPT Sat Aug 30 17:39:19 BST 2014 armv6l GNU/Linux



firmware revision --+

On Raspbian, the standard upgrade procedure should keep your firmware up to date:

$ sudo apt-get update

$ sudo apt-get upgrade


Previously, these documents have suggested using the rpi-update utility to update the Pi's firmware; this is now discouraged. If you have previously used the rpi-update utility to update your firmware, you can switch back to using apt to manage it with the following commands:

$ sudo apt-get update
$ sudo apt-get install --reinstall libraspberrypi0 libraspberrypi-{bin,dev,doc} raspberrypi-bootloader
$ sudo rm /boot/.firmware_revision

You will need to reboot after doing so.

After the update, I installed:

apt-get install python3-picamera

apt-get install libav-tools

man python3


Python is an interpreted, interactive, object-oriented programming

language that combines remarkable power with very clear syntax. For

an introduction to programming in Python you are referred to the

Python Tutorial. The Python Library Reference documents built-in and

standard types, constants, functions and modules. Finally, the

Python Reference Manual describes the syntax and semantics of the

core language in (perhaps too) much detail. (These documents may be

located via the INTERNET RESOURCES below; they may be installed on

your system as well.)

Python's basic power can be extended with your own modules written in

C or C++. On most systems such modules may be dynamically loaded.

Python is also adaptable as an extension language for existing appliâ

cations. See the internal documentation for hints.

Documentation for installed Python modules and packages can be viewed

by running the pydoc program.

So far this first script works the camera directly with:

python3 /

but exact use will depend on your initial environment choice from the links above - OPENCV, etc. - and whether you "sandbox" it using that or not. I just wanted to copy and paste a script into a vim text doc and be able to run it, and that is working so far using the code example in a text doc:

vi /


import picamera

from time import sleep

camera = picamera.PiCamera()



camera.vflip = 0

camera.hflip = 0

camera.brightness = 60





python3 /

You should get about a 5sec TV output from the camera.

Now, this is where it gets interesting! How to get that stream into a file for viewing after?

I tried various crude pipe methods (I'm not a script kid either!) like:

python3 / >> /Share/pythonpi1.h264

python3 / pipe:1 | /Share/pythonpi1.h264 (from the ffmpeg man page)

python3 / | cat > /Share/pythonpi1.h264

to no avail, but what did work, with complaints, was:

python3 / | raspivid -o /PiShare/pythonpi1.h264

mmal: mmal_vc_component_enable: failed to enable component: ENOSPC

Traceback (most recent call last):

File "/", line 4, in <module>

camera = picamera.PiCamera()

File "/usr/lib/python3/dist-packages/picamera/", line 488, in __init__

self.STEREO_MODES[stereo_mode], stereo_decimate)

File "/usr/lib/python3/dist-packages/picamera/", line 620, in _init_camera

prefix="Camera component couldn't be enabled")

File "/usr/lib/python3/dist-packages/picamera/", line 191, in mmal_check

raise PiCameraMMALError(status, prefix)

picamera.exc.PiCameraMMALError: Camera component couldn't be enabled: Out of resources (other than memory)

I can look at all that fault output later for reasons and understanding, but for now, just using what I know so far, I have something functional and an incentive to build on, for the overall goal of getting surveillance setup at work sometime but having a bit more working knowledge and camera familiarity, even if using another App like ZoneMinder (zm) or other, and following someone else's instructions.

Add the other lines in for the fade from light to dark also, and uncomment them to see the effect:


import picamera

from time import sleep

camera = picamera.PiCamera()



camera.vflip = 0

camera.hflip = 0

camera.brightness = 60

#for i in range(100):

# camera.brightness = i

# sleep(0.1)





The uncommented version does not save the fade output to the vid file oddly, so I can't show it, it's just a normal vid...?

You can also run vids on the Pi with:

omxplayer /PiShare/Buck.h264

Get the whole movie with sound in a format to suit here:

avplay /PiShare/
avplay version 9.14-6:9.14-1rpi1rpi1, Copyright (c) 2003-2014 the Libav develope
built on Jul 22 2014 15:08:12 with gcc 4.6 (Debian 4.6.3-14+rpi1)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/PiShare/
major_brand : qt
minor_version : 512
compatible_brands: qt
encoder : Lavf54.20.4
Duration: 00:09:56.45, start: 0.000000, bitrate: 3761 kb/s
Stream #0.0(eng): Video: msmpeg4v2, yuv420p, 1280x720, 3556 kb/s, 24 fps, 24
tbr, 24 tbn
Stream #0.1(eng): Audio: aac, 48000 Hz, stereo, fltp, 200 kb/s

If you have QT - guess what?

MOV H264 video, AAC surround sound – Apple Quicktime Compatible. (has audio playback problems with VLC and possibly other non-QT players on stereo systems; VLC users should choose one of the other formats)

If you want picture filter/effect options, there is script for 6 pics here, with 90 degree tilt for some reason:


then run:


Just found a series of pages that cover a lot of requirements for streaming and surveillance:

...but you have to buy them..duh...

Basic streaming from that to another host:

MintPC stevee # nc -l -p 4444 >> Downloads/pistreamtest.h264

root@pi:/home/stevee# raspivid -t 0 -o - | nc mintpc 4444

MintPC stevee # ls -als Downloads/pistreamtest.h264
61180 -rw-r--r-- 1 root root 62648320 Aug 5 23:48 Downloads/pistreamtest.h264

I could play back this streamed and saved video with VLC; after a directory perms change:

MintPC stevee # vlc Downloads/pistreamtest.h264
VLC is not supposed to be run as root. Sorry.
If you need to use real-time priorities and/or privileged TCP ports
you can use vlc-wrapper (make sure it is Set-UID root and
cannot be run by non-trusted users first).
MintPC stevee # exit
stevee@MintPC ~ $ vlc Downloads/pistreamtest.h264
VLC media player 2.1.6 Rincewind (revision 2.1.6-0-gea01d28)
Failed to create secure directory (/run/user/1000/pulse): Permission denied
[0xeb85e8] pulse audio output error: PulseAudio server connection failure: Connection refused
Failed to create secure directory (/run/user/1000/pulse): Permission denied
[1]+ Stopped vlc Downloads/pistreamtest.h264
stevee@MintPC ~ $ sudo mkdir /run/user/1000/pulse
[sudo] password for stevee:
mkdir: cannot create directory \u2018/run/user/1000/pulse\u2019: File exists
stevee@MintPC ~ $ chmod 755 /run/user/1000/pulse
chmod: changing permissions of \u2018/run/user/1000/pulse\u2019: Operation not permitted
stevee@MintPC ~ $ sudo chmod 755 /run/user/1000/pulse
stevee@MintPC ~ $ vlc Downloads/pistreamtest.h264
VLC media player 2.1.6 Rincewind (revision 2.1.6-0-gea01d28)
[0x885118] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[h264 @ 0x7f9640c20c20] Estimating duration from bitrate, this may be inaccurate
"sni-qt/5496" WARN 00:03:27.625 void StatusNotifierItemFactory::connectToSnw() Invalid interface to SNW_SERVICE
[0x7f9640c0f8d8] main input error: Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !
Fontconfig warning: FcPattern object size does not accept value "0"
Fontconfig warning: FcPattern object size does not accept value "0"
Fontconfig warning: FcPattern object size does not accept value "0"
Fontconfig warning: FcPattern object size does not accept value "0"
[0x7f9610001248] main vout display error: Failed to resize display

There is a whole management package for xxx Euros I am researching now, here:

Comments are closed.

Post Navigation