Image Steganography in Python

Second Title: Hiding Secret Messages in Cute Pictures of Dogs

What is this pupper hiding???

I developed this activity to as an interesting way to introduce students taking the foundation level software class at Olin to image manipulation and binary math. Since I spent quite a bit of time working on it, I wanted to publish the entire thing as a blog post in case other people on the web are interested in steganography. This exercise was modified from a similar one found at Interactive Python, though this version encodes an image into another image instead of ASCII text.

Because I wrote this as a learning exercise, there’s a neat little repo that holds starter code and solution code. You can clone/fork it here. The starter code is in steganography.py and has some missing functions for you to fill out, while the full solution is in solution.py. There are two main functions to this code. The first is a decoding function that can extract secret information from an image file, while the second is a function that can encode secret messages into images.

This code uses the Python Pillow library. You can install it using pip if the package is missing from your computer by typing sudo pip install pillow into your terminal window.

If you don’t wanna learn about how to do steganography and just want a neat script that will hide messages in images for you, you can use the code in solution.py. It is also embedded at the end of this post.

What is steganography?
In a nutshell, the main goal of steganography is to hide information within data that doesn’t appear to be secret at a glance. For example, this sentence:

Since everyone can read, encoding text in neutral sentences is definitely effective

turns into

Secret inside

if you take the first letter of every word. Steganography is really handy to use, because people won’t even suspect that they’re looking at a secret message–making it less likely that they’ll want to try to crack your code. In the past, you may have tried to accomplish this kind of subterfuge using invisible inks or using special keywords with your friends. However, as fearless coders we have access to fancier ways to sneak data around. *evil laughter here*
[Read more…]

Algorithm for solving Knights Tour with Python(revisited)

The original Algorithm For Knights Tour was a pretty popular post, so I decided to revisit it thanks to an interesting email I got. This implementation has a couple changes:

  • You can define a ruleset for locations the knight must be at any given move
  • You can define whether the path needs to be closed or not (Knight returns to starting position)
  • There’s a visualizer now! (You’ll need to have pygame installed for it to work though)
  • No more ugly exiting using sys.exit(), we catch custom exceptions this time around

Here’s what the visualizer looks like:
(pink because pink is the best color obvs)

tour.py

The tour code is mainly the same as the original post, but with the added custom exception class and booleans. Lines 137-146 deal with obeying to a custom tour ruleset 
[Read more…]

Sync iTunes playlists to Spotify

iTunes is dumb and has a super restrictive API. Spotify is awesome, and has an API for all my things, like my PS4 and Amazon Echo. But I like the iTunes UI, and it plays better with my local library than Spotify. As a result, I subscribe to both services, but it’s such a pain managing playlists between the two.

I present to you, the super not janky at all sync script/setup that I wrote up in a 3 hour burst of inspiration last night. The main flow of the idea is this:

  1. CRON job runs AppleScript periodically
  2. Applescript queries iTunes for playlist information and saves each playlist and its songs to a .txt file
  3. Python script compares .txt files to Spotify playlists – information retreived via API calls
  4. Python script updates Spotify playlists with differences found between the two services

AppleScript is a very interesting programming language, where the code is incredibly human readable at the cost of ease of use when coding. Here’s what I wrote for extracting playlist information out of iTunes:

This extracts playlist information and saves each playlist as a .txt file to a specified directory. The Python component does most of the legwork in terms of finding which tracks needed to be added to Spotify. You’ll need to create a new application at the Spotify Developers page to get a client_id and client_secret_key.
[Read more…]

Teaching cars to drive with genetic algorithms

“What the heck Sophie?!” You say, “Why are you writing a debrief for a project you did 3 years ago???”

Hahaha, please bear with me dear reader. If I don’t, I’ll forget about the cool thing I did as a freshman in college still stumblin’ through Python. And how cool and easy genetic algorithms are. πŸ˜€

Anyhow, carrying on….


For our software design final project, we wrote a simulation program that would allow you to draw a track, then generate cars that would eventually learn how to drive around said track. It was a pretty fun project, and we got a pretty cool result.

Here’s our website!
Here’s the code!

Demo video:

What’s happening is that the cars are starting with random parameters for their wheels relative to its distance from the wall. As the the cars progress through the generations, these parameters are selected using our parameters, such that the car that make it around the track is the only one that survives.

[Read more…]

Installing and setting up ROS in 5 mins

[*disclaimer: not really 5 mins. Downloading ROS will probably take longer than that lol]

ROS is a really great library for robotics research. It might even be good for not robotic applications, I don’t know, but my general feel is that I like ROS about as much as I like nutella. (read: a lot). The ROS install tutorial is really good at explaining the process, but it’s spread over too many pages and requires too much scrolling. I’ve consolidated the information here in one page for ease of use. Our robotics lab only uses ROS Indigo, so I’m writing install instructions with that in mind. Also, ROS only works well with Ubuntu, so I’m assuming you’re also using the correct OS.

Everything that follows, unless explicitly stated, should be typed in a terminal window. Okay, so the steps:

1. Set up sources.list
We need to add the ROS package sources to our sources list so we can actually find the ROS packages using the apt package manager. Open up a terminal window and paste the following:
sudo sh -c ‘echo “deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main” > /etc/apt/sources.list.d/ros-latest.list’

2. Set up your keys
Next we need to set up keys so Ubuntu will actually allow us to download + install the packages.
sudo apt-key adv –keyserver hkp://ha.pool.sks-keyservers.net –recv-key 0xB01FA116
If you get a gpg: keyserver timed out error, use this instead:
sudo apt-key adv –keyserver hkp://ha.pool.sks-keyservers.net:80 –recv-key 0xB01FA116

3. Install ROS via apt-get
Make sure the apt package index is up to date from when we added those new sources at step 1.
sudo apt-get update
Then install ROS!
sudo apt-get install ros-indigo-desktop-full
There are a couple of flavors of ROS that you can install, but for most applications, you might as well do the full install to make sure you get all the packages you’ll ever need.
[Read more…]

Algorithm for knight’s tour in Python

I’ve written an updated version of this code here. Please take a look! It adds a visualizer, ability to solve for closed tours and step requirements, and has a better exiting strategy. πŸ™‚


In Discrete class, we’ve been talking about planar graphs and stuff like Hamilton traversals. One of our in-class exercises involved the knight’s tour, and whether we could find a rule that would allow us to decided if a knight’s tour was possible given a chessboard of a certain dimension. I have a hard time understanding graphs, but the “Knight’s Tour” sounded really cool so I looked it up on Wikipedia. The problem is actually a pretty interesting one, so I decided to try my hand at implementing an algorithm for solving it in Python.

From Wikipedia:
A knight’s tour is a sequence of moves of a knight on a chessboard such that the knight visits every square only once. If the knight ends on a square that is one knight’s move from the beginning square (so that it could tour the board again immediately, following the same path), the tour is closed, otherwise it is open.

The .gif below is an example of what knight’s tour would look like on an 8×8 board.
Gif of knight's tour

In writing my own implementation, I heavily referenced this site and this site. I’ll do a walkthrough of my code below.

[Read more…]

Python speech to text with PocketSphinx

I’ve wanted to use speech detection in my personal projects for the longest time, but the Google API has gradually gotten more and more restrictive as time passes. In order to ensure that my projects could work even without an internet connection, I looked for another speech recognition package that would preferably be easier to use. I found the Sphinx voice recognition suite of CMU to be a really great speech to text package. However, documentation and sample code is non-existent, so it took me forever to get anything done. Finally, I’ve figured it out! The example code is at the bottom of this post, but you can directly download it from Github here.

Here are the steps to take to get this working:

  1. Download SphinxBase and follow the install instructions
  2. Download PocketSphinx and follow the install instructions
  3. Download PocketSphinx-python and follow the install instructions
  4. Run the code below

[Read more…]

N-link compound pendulum simulation

For our Mechanical Dynamics final project, a friend and I decided to generalize the equations for compound pendulums to write a simulation program that could generate animations for n-link compound pendulums with user specified lengths, masses, angular displacement and velocities. We originally wanted to write a full blown physics engine, but quickly realized that two weeks was not enough time to do something of that scale, haha.

First things first, a demonstration! The following is an simulation of a 5 link compound pendulum:

Read our final report for a more formal/comprehensive explanation of the process!
Download the code!

[Read more…]

PseudOS — A baremetal RaspPi OS

Now that I’ve got a bit of time, I thought I’d do a debrief of a couple of cool projects that I’ve worked on this past year. First up, PseudOS, a baremetal Raspberry PI OS that I wrote with some friends for our Software Systems final project.

(We were really sleep deprived when making the final video, apologies for any strange noises we make.)

It doesn’t look really amazing, because we didn’t really implement any OS features other than UART and a basic calculator program. However, we did do this all baremetal. What is baremetal, you ask? Well, it means that the code we wrote interacted with the RaspPi at the hardware level. We modified an Assembly boot file to give us control over the hardware registers, and wrote a kernel in C to provide the actual OS functionality. This website was an amazing resource for us through the whole process.

[Read more…]

Installing OpenCV for C++ and Python

Copying the instructions from my awesome robotics professor Paul Ruvolo from here so I don’t lose these steps:

To install OpenCV, go ahead and type each of these steps into your terminal window. Note that the “make” step will take forever, so go grab some popcorn or tea once you get there.

$ cd /tmp
$ wget https://github.com/Itseez/opencv/archive/2.4.11.zip
$ unzip 2.4.11.zip
$ cd opencv-2.4.11
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_BUILD_TYPE=RELEASE \
-DBUILD_PYTHON_SUPPORT=ON \
-DWITH_XINE=ON \
-DWITH_OPENGL=ON \
-DWITH_TBB=ON \
-DBUILD_EXAMPLES=ON \
-DBUILD_NEW_PYTHON_SUPPORT=ON \
-DWITH_V4L=ON \
-DOPENCV_EXTRA_MODULES_PATH=./modules
$ make -j2
$ sudo make install

In a nutshell, we download OpenCV 2.4 to the temporary folder, build it, make with two CPU cores, then install.Β It seems like those cmake parameters are actually pretty important to get stuff working!

-Sophie