Glitch Webcam

Glitch Webcam* is a small script that was developed during my time at Databit.me as part of the Open Camera project, which aimed at finding inexpensive ways to take images. Since then it has been in the MEMIC exhibition in November 2012 and usually makes an appearance wherever my laptop goes.

At only five lines of code and ~254 bytes, this script is a very quick way to glitch photos taken automatically by a webcam. The project was originally demoed using a Raspberry Pi/Raspbian and a digital photo frame, though problems with USB have prevented me from doing that since.

Below is the script, which requires you to have installed Streamer, feh (1.3.4 used), sed and xdotools:

#!/bin/bash
mkdir output & streamer -s 640x480 -o ./output/glitch_00000000.jpeg -t 1000000000000 -r 0.48 &
sleep 1.8 ; feh --action1 ";sed -i s/g/2/ %f" -F -r ./output/ --force-aliasing -Z -Y --reload 1 --slideshow-delay 2 &
while true ; do xdotool key --delay 200 1 ; done

The script works by using Streamer to capture sequentially-numbered images to a folder called “output”. feh is then used to display the image and also to run a sed command, thanks to the –action1 option. Actions in feh can only be run via a key press, so xdotools is used to automatically press a key (1). feh is then finally used to display each image full-screen.

It’s not the most efficient way of glitching from a webcam – e.g. you can get glitch plugins for Pure Data, Quartz Composer and other software – but as a small utility it works well. Also, it saves all of the images so you can make an animation out of it!

Glitch Webcam

*terribly unimaginative name, I know!

Streams of data

One of my overall goals is to find a way to databend live video. I’m sure there’s a way to do it with Processing and PureData but I’m not yet proficient in those programs so they’re out of the question for now. In the meantime I thought to try and hack the Echobender script to databend my webcam images.

>tonyg provides a great tutorial on how to convert live webcam images into audio, which I’ve used as a starting point for my hack.

The process for making it works is as follows:

  • Images from the webcam are saved to the computer
  • These are converted to a .bmp file then renamed to a .raw file
  • Sox applies an audio effect to the .raw file
  • The .raw file is converted back to a .bmp then to a .jpg
  • The updated webcam image is displayed to a window and updated once every second

Sound overly complicated? It probably is. Like the Echobender script you’ll need ImageMagick and Sox but we’ll also be using Webcam, which you can install via sudo apt-get install webcam

If you haven’t already, create a file called .webcamrc in your home directory (/home/yourusername) and enter this text into it:

[grab]
delay = 0
text = “”

[ftp]
local = 1
tmp = uploading.jpg
file = webcam.jpg
dir = .
debug = 1

Now create a file called grabframe, place it in your home directory and fill it with this:

#!/bin/sh

while [ ! -e webcam.jpg ]; do sleep 0.1; done
convert webcam.jpg frame.bmp
cp frame.bmp frame.raw
sox -r 482170 -e u-law frame.raw frame2.raw echos 0.8 0.9 5000 0.3 1800 0.25
convert -size 640x240 -depth 4 rgb:frame2.raw -trim -flip -flop output.bmp
convert output-0.bmp output.jpg

To start things running, open up three terminal instances:

  • In shell number one, run webcam.
  • In shell number two, run “while true; do ./grabframe ; done.
  • In shell number three, run display -update 1 output.jpg

Voila!

I know it’s quite slow, but I haven’t yet found a way to update faster and it’ll still be restricted by the time it takes Sox/ImageMagick to perform their conversions.

Thanks again to tonyg, Imbecil and Mez for their help and inspiration