Category Archives: computing


Python script to upload to a specified directory

Whenever I need to solve a practical computing problem that requires writing a script, tracking down the right tool for the job is often the trickiest part. Especially when I am learning a new language, it is easy to fall down the rabbit hole of documentation and extra libraries (not to mention threads on discussion forums).

Today, I wanted to write a script that would automatically upload a particular file on my local machine to my server that hosts my website. Basically, I got tired of constantly having to remember and type out the (long) destination path for every file I wanted to upload. Since I am learning Python, I thought I would figure out how to solve the problem in Python.

After doing some research, I found the Plumbum library offers exactly the tools I needed to get the job done. Since I didn’t have Plumbum installed on my machine, I installed it using pip.

pip install git+git://

Now with Plumbum installed, I was able to put together this code which does exactly what I want so far:

#!/usr/bin/env python

import plumbum
import sys

host = ""
username = "username"
key = "/path/to/my/public/key"

local_prefix = "/home/username1/Dropbox/"
remote_prefix = "/home/username2/Dropbox/"

filename = sys.argv[1]

r = plumbum.machines.SshMachine(host, user = username, keyfile = key)

fro = plumbum.local.path(filename)
remote_path = str(fro).replace(local_prefix, remote_prefix)
to = r.path(remote_path)

plumbum.path.utils.copy(fro, to)

Now whenever I want to copy a file from my local machine to its corresponding directory on my server, all I have to do is run

noga-sync filename

instead of the old way

scp filename

I think it’s a small victory for efficiency, but a victory nonetheless.


Building a Photo Booth: Part IV


In the previous posts (Parts I, II and III) I discussed setting up the Raspberry Pi and camera, taking and compiling photos to make a photostrip, and automatically uploading pictures to Flickr. In this post we deal with hardware. The input/output for the photo booth should be exceptionally simple: a light will indicate that the photo booth is ready to take pictures and a single button will start taking pictures.

The Raspberry Pi basic input/output easy through the GPIO pins on the Pi’s board. I found the following tutorials very helpful: Morse Code on an LED and Buttons and Switches. Using these tutorials as a guide, I found that the following circuit should allow all of the functionality the photo booth requires:


Here is what the test circuit looks like in real life:


In order to get the circuit to communicate with the Pi, I used the GPIO library for Python included in the Pi’s operating system. Here is a snippet of code that will wait for the button to be pressed, then flash the LED and take a picture

import time
import RPi.GPIO as GPIO
from subprocess import call

# set input/output pins
out_pin = 4
in_pin = 17

# wait time parameters
pause_time = 0.5
bounce_time = 0.05

# setup GPIO pins
GPIO.setup(out_pin, GPIO.OUT)
GPIO.setup(in_pin, GPIO.IN)

# take pictures -- this will be more elaborate later
def take_pictures():
	call(["raspistill", "--timeout", "1000", "--output", "pic1.jpg"])

# wait for button press, flash light, and take a picture
prev_input = 0
while True:
	input = GPIO.input(in_pin)
	if ((not prev_input) and input):
		print("Button Pressed")
		GPIO.output(out_pin, GPIO.HIGH)
		GPIO.output(out_pin, GPIO.LOW)
	prev_input = input


Everything worked with the circuit on the breadboard, so I decided to solder the circuit onto a small perf board. The end result turned out looking much nicer despite my rather crude soldering.


In the next (and final) installment, I will put all of the code together for a fully functional photo booth. Hopefully I will have time to make a pretty box to house it.


Building a Photo Booth: Part III

So far (Part I, Part II) I set up my photo booth to take, edit, and combine images to form a photostrip. In order to share the pictures with friends and family, I wanted the photo booth to automatically upload the images to a social media site. I settled on using Flickr because it allows controlled access (pictures can be made private) and it has relatively user-friendly API. The API allows my photo booth software to automatically upload pictures as they are taken.

After a lot of sleuthing and tinkering, I found that this Python implementation of the Flickr API was my best bet. Once everything is set up with Flickr, the API allows you to upload images to your Flickr account with just a few lines of code.

Registering the photo booth with Flickr Before an application has access to any Flickr accounts, it must be registered through Flickr. You can follow the instructions here to register your application. The registration will give you a public and secret key that allow Flickr to authenticate your application. Keep these in a safe place.

Giving the photo booth access to your Flickr account In order to upload pictures to a Flickr account, you need grant the photo booth application access to your account. Here are some instructions on how to accomplish this. Be sure to set the permission ("perms") to "write" (instead of "read" as in the example).

Uploading Images In order to upload images to your Flickr account, you need to store the authentication keys associated with that account somewhere. Mine are stored in a file called “auth-handler” in the same directory as the python code for my photo booth. Here is the code used to upload an image to my Flickr account:

import flickr_api

flickr_api.set_keys(api_key = 'MY_KEY', api_secret = 'MY_SECRET')

user = flickr_api.test.login()

flickr_api.upload(photo_file = "/home/pi/photobooth/pics/tile.jpg", title = "Test Picture")

After running this code, I looked at my Flickr photo stream and this is what I found.

So far everything seems to be working well! Next time, I will be putting together the hardware associated with my photo booth: flashing lights and buttons.


Building a Photo Booth: Part II

In the first installment, I went over the basic setup of my Rpi which is the brain of the photobooth. Today, I am installing some software to process photos, and writing a script which will take, edit and print the photos.

Editing Pictures I will be using ImageMagick to edit the photos. ImageMagick is a powerful suite of image manipulation tools which can be run entirely from the command line, making it perfect for the photobooth. To install ImageMagick on the Rpi, run the command sudo apt-get install imagemagick

ImageMagick includes a tool called “mantage” which combines several images into a single image. I wrote a simple shell script which takes 4 pictures at regular intervals, then combines the images into a single picture:

# Filename: take-photos
# Description: takes 4 photos at regularly spaced time intervals
# and combines the images into a single 2 x 2 montage

# Take 4 pictures with 1 second delay between the pictures
raspistill --width 504 --height 504 --timeout 5000 --quality 75 --output pic1.jpg
raspistill --width 504 --height 504 --timeout 1000 --quality 75 --output pic2.jpg
raspistill --width 504 --height 504 --timeout 1000 --quality 75 --output pic3.jpg
raspistill --width 504 --height 504 --timeout 1000 --quality 75 --output pic4.jpg

# Combine images into a single montage
montage pic[1-4].jpg -tile 2x2 -geometry 504x504+4+4 tile.jpg
convert tile.jpg -density 256 tile.jpg

Here is the output of my first test using take-photos:


Next time, I will go over how to automatically upload the photos to Flickr.


Computing Round-up

For today’s computing round-up, I have a few tips that came in handy.

  • The LaTeX algorithmic and algorithm environments make nicely formatted pseudocode for describing algorithms. See the link above for details on usage
  • To recover an auto-saved file from Emacs, use the command “M-x recover-file”. That was a life-saver today when my internet connection decided to crap out. One of the perils of doing all my work remotely through SSH, I suppose.
  • Use Emacs Org mode to organize projects and to-do lists. I’m just starting to get my bearings, but I can already see how it is going to be phenomenally useful for larger projects. Some helpful links: official website; quick tutorial.
  • Unfortunately Org mode has some conflicts with yasnippet (another Emacs tool that I am quite fond of). Here is a link to some documentation on how to fix it.

More Colors on iSSH

A few years ago, I started using an iPad with an external keyboard for most of my day-to-day computing. On app that I’ve found completely indispensable is iSSH, which allows me to use SSH to log into my server from the iPad. I noticed a while ago that the terminal on iSSH didn’t have the same color palette as the terminal app on my desktop. I thought this was a limitation of iSSH, but it turns out that I was wrong. You can actually change the xterm session in iSSH to xterm-256color, which makes everything look much nicer — especially in emacs with text highlighting (see this previous post). To change the settings in iSSH, go to the “edit configuration” page for the connection you are trying to edit and go to SSH -> Shell -> Advanced and change the “Term String” to “xterm-256color”.


Here’s the before:


And after:


Much nicer, eh?


Building a Photo Booth: Part I

As a project for a certain event this summer, I’ve decided to try my hand at building a photo booth. Like a traditional photo booth, my photo booth should take several pictures separated by a few seconds, then print the photos. Additionally, I think it would be neat to also post the pictures automatically to some social media like twitter or instagram. After doing some research, I found that my best option for building such a photo booth was to use a Raspberry pi (Rpi) along with the optional camera attachment. Together, the Rpi and the camera cost around $70. To print the pictures, I bought a used photo printer (an Epson PictureMate) off of ebay for another $35.

Setting up the Rpi The Rpi is meant to be very easy to set up. However, my setup was a bit complicated by the fact that I didn’t want to buy an external keyboard and monitor for my pi. Fortunately, I found some tutorials for setting up the computer over my wireless network. The first step is to format the SD card with the operating system for the Rpi. I followed the instructions here. Once the SD card was formatted, I followed the instructions here to set up my pi over my local network. Finding the IP address of my pi on the network proved to be a bit of trouble. I ended up downloading a tool called LanScan which easily found the IP address for me.

Once my pi is turned on and plugged into my router, I can now log in via ssh from any computer on my network. So far so good. Just remember to use “sudo shutdown -h now” to shut down the pi before unplugging it!

Installing the camera The camera installation turned out to be the easiest part of the whole process. I just followed the instructions here. To take a picture, use the command “raspistill -o foo.jpg” (replacing “foo.jpg” with your desired file name). By default the camera waits 5 seconds before taking the picture. A little red light comes on on the camera to let you know it is taking the picture. Here is the first test photo I took of Alivia and me. We both look pretty tired:

test picture

Installing the printer Installing the printer also turned out not to be terribly difficult. Again, it was slightly complicated by needing to do everything remotely, but fortunately there were some easy workarounds. General instructions for installing CUPS (a Unix printing system) can be found here. I followed the instructions here to edit the configurations files for CUPS to allow remote setup. With the setup complete, I was able to print the test photo I took earlier. Here is the result:


On the left is the Rpi itself (it’s about the size of a credit card!). The printing was quite slow, but I think it will be faster if I use a lower resolution.

My work on the photo booth is just starting, but so far everything has gone relatively smoothly. In future posts, I will continue to describe my foray into DIY electronics. Next on my agenda is to install a wireless adapter and figure out how to use command line tools to edit the photos.

In the next post, I will go over how to use ImageMagick to edit and compile images into a single photostrip.


Site down

I received an email from a student informing me that my website was down last night. Apparently there was a problem with the database (MySQL was not running). After doing some troubleshooting, I attempted what should have been the first thing I tried: restarting the server. Seemed to do the trick.


Emacs: My favorite text editor

Emacs Screenshot
For most of my text editing needs, I use Emacs. Emacs is an open source text editor freely available for most Unix-like operating systems (including Mac OS X). It is extremely powerful and infinitely customizable. One of the overriding philosophies of Emacs is that one should not need to ever use the mouse when editing a text file. Using only the keyboard feels a bit cumbersome at first, but once you have a few keystroke patterns in you muscle memory, it seems much more efficient that using the mouse.

Emacs has a thriving community of people devoted to extending Emacs and supporting other Emacs users. Thus there are tweaks and modifications that allow you to do almost anything you can imagine doing with a text editor (even browsing the web — text only). To get started with Emacs, there is a very well laid out EmacsWiki.

Here are a couple of plugins that I have gotten a lot of use from recently:

  • Color theme allows you to change the colors Emacs uses when writing code. Here is a gallery of some of the preset color themes. I’m a fan of “Blue Mood.”
  • Yasnippet allows you to create and use keyboard shortcuts (specifically tab triggers) when using Emacs. Before switching primarily to Emacs, I used TextMate for OS X. TextMate is strongly influenced by Emacs, but also includes tab triggers to run macros. A few Emacs people like this feature so much the implemented the same functionality in Emacs, resulting in Yasnippet.

I haven’t gotten too much into doing my own customization of Emacs, but I very much like the idea that I can modify however I want. It even comes with its own programming language!


Make a Centralized Bibliographic Database with BibTeX!

Like most working mathematicians I know, I use LaTeX for all of my mathematical typesetting. Until recently, the vast majority of my writing was for assignments for classes and personal essays where I didn’t need to cite any sources. Fortunately there is a wonderful tool called BibTeX that easily manages citations for when inevitably I need to cite others’ work. I have started compiling a centralized database of all the papers and books that I cite in my own writings. All of the sources are in a single file (which I called sources.bib). In order for BibTeX to find sources.bib whenever I reference it, I placed the file in my local texmf tree:


Now any time I feel the need to cite, for example, Cover and Thomas’ Elements of Information Theory, all I need to do is put


in my .tex file and BibTeX takes care of the rest.