A quick and beautiful hack!
Monday, 14 May 2012

You can get inspired from the most unlikely of places, and this time it was a twitter background image. I came across the twitter profile of @Nitish today, a fellow GSoC student from India. One thing that held my attention, apart from the fact that he tweets crazy, was the background picture in his profile. Yes I had not seen something like that before and I absolutely loved the concept.

The image was large grid and each tile carried the profile picture of one of his twitter followers. It was beautiful. The background image was generated by twilk, a very popular application that I had never heard of. I wanted to try it, but I figured my follower count might be a little too low for the application to work. Twilk works only with twitter and I wanted it for Facebook. Instead of looking for something similar, I decided to write it myself. I had worked with the Python Imaging Library some time back and knew it would not be very difficult task.

The first thing I needed was the profile pic thumbnails of my friends. Facebook API supports a call which returns a small 50x50 pixel profile pic of a user, if we send a valid user ID. I wrote a quick script that returned the user ID’s of my friends on Facebook as JSON data. I grepped the user ID’s from the JSON and wrote a small wget script to download the profile pics of all my friends to a directory.

Once the download was in progress, I started my work with the imaging library. I knew the image count beforehand and after some quick math, I fixed the dimensions of the output image that I wanted. Once the dimensions were fixed, the work was fairly easy. I knew before hand how many images will be there in each row and how many rows will be there in total. All I had to do was to randomly pick one of the images from the directory, and arrange it on the output image canvas. Few lines of code later I get the result that I wanted and it is on my twitter profile. Yay! :)

I have uploaded the python code in here. If you are running it, make sure you change the values according to your requirements. The image below is a cropped out from the output image to fit this div. Click on it to see the actual output.

When I showed this to @Jaseeemabid, he asked me to put this up on Google App Engine and start a service so that even non geeks could use this. I liked the idea but I am slightly busy(lazy) now. I’ll do it when I am free or when I see that there is some demand for this! :)

Update

As few Redditors pointed out, this may not be the best way to do it. This was just a quick hack to get something done. The JSON could be processed in Python itself and files can be downloaded as well. I modified an old PHP script to get the JSON data and since the structure was relatively simple, I grepped the relevant information and fed it to wget as I prefer wget over any other tool for mass downloads.

Update 2

Redditor Bio Lazarus improved upon this and made an end to end version that does all of the above in one single python script and the code is available here.

And if you like this post, you should follow me on twitter :)

blog comments powered by Disqus