Example Photographic MadLibs
Did you ever do MadLibs as a kid? You're asked to supply, say, a name, a food, a verb, and a place. Then the words you supply get plugged into the blanks of a story, with often bizarre and sometimes humorous results e.g., like "George W. Bush likes to snort largue chunks of chocolate while visiting The Louvre."
Here's an example of doing that same idea, but with pictures.
Here's the first version. The pictures "Mark.jpg","Barbara.jpg","beach.jpg", and "ken.jpg" were all put into the same directory where madlib.html was to be stored (in this example, right in the same directory as JES). I then called:
madlib("Mark.jpg","Barbara.jpg","beach.jpg","ken.jpg")
And here's the result: madlib1.html
The fun part of madlib is to try different sets of words in the same story. So, here's the same story again, but plugging in:
madlib("Mark.jpg","Bird.jpg","moon.jpg","horse.jpg")
madlib2.html
The Code
Here's the program that generates these: madlib.py
Notice that the input could be any valid URL – the pictures don't have to be on your disk at all!
How it works
- The first thing we do is to open the file for writing.
file=open("madlib.html","wt")
- Next, we make some HTML segments for referencing the input URLs as images. Notice the use of single 'quotes' to define the strings, so that we can have "double quotes" inside the single quotes.
person1='<image width=50 src="'+person1url+'">'
person2='<image width=50 src="'+person2url+'">'
place='<image width=100 src="'+placeurl+'">'
otherperson='<image width=50 src="'+otherpersonurl+'">'
- The rest of the code is pretty straightforward–we simply write to file all the strings that make up the HTML file. The trickiness is
- Intermixing strings with "+" and the variables referring to the images, and
- in careful choice of quote marks so that double quotes don't get misinterpreted as string delimeters.
file.write("<p>"+person1+" said to "+person2+'"I know this great place: '+place)
file.write('Will you meet me there?"')
Link to this Page
- Mark Guzdial last edited on 19 October 2006 at 1:51 pm by lawn-128-61-114-152.lawn.gatech.edu