943,193 Members | Top Members by Rank

Ad:
  • Python Tutorial
  • Views: 17428
  • Python RSS
4

Multimedia: introducing the webbrowser module

by on Jun 8th, 2006
Probably the simplest way to get into multimedia with Python is the webbrowser module. This works with both Windows and Unix systems, and selects the default browser you have installed. But that is not all: if you give it the name of a multimedia file it will select the default viewer or player, at least in Windows systems.

An example sound file might look like this:
python Syntax (Toggle Plain Text)
  1. import webbrowser
  2. # opens .wav file with default wave sound player
  3. webbrowser.open("Train.wav")

Whereas to display a bitmap graphics file you would have something like:
python Syntax (Toggle Plain Text)
  1. import webbrowser
  2. # opens a .bmp file with default picture viewer
  3. webbrowser.open("Train.bmp")

But the real power of the webbrowser module can be seen when you realize that it will load the browser with HTML code that can quickly be created from within a Python script. What this gives you is the ability to combine a picture with some sound, as highlighted in the following example:
python Syntax (Toggle Plain Text)
  1. import webbrowser
  2. # html code to show a picture with sound effect
  3. # you can replace files train.bmp and train.wav in the code
  4. # with an image (.bmp, .gif, .jpg) and sound file (.wav, .au) you have
  5. # you may need to change width and height to fit your own picture
  6. # make sure the image and sound files are in the working folder
  7. str1 = """
  8. <html>
  9. <head>
  10. <title>Bring up an image with sound</title>
  11. </head>
  12. <body>
  13.  
  14. <img src = "train.bmp" width=320 height=210 border=5>
  15.  
  16. <bgsound src = "train.wav" loop=2>
  17.  
  18. </body>
  19. </html>
  20. """
  21.  
  22. # write the html file to the working folder
  23. fout = open("SoundImage.htm", "w")
  24. fout.write(str1)
  25. fout.close()
  26.  
  27. # now open your web browser to run the file
  28. webbrowser.open("SoundImage.htm")

Yet the flexibility of the webbrowser module goes even further, because simple HTML code can be used to show several pictures at once. How about having two animated gif graphics next to each other to spell HI? Even if your default viewer didn't work with animated gifs, this will work because browsers just love animated gifs. Here is the example code:
python Syntax (Toggle Plain Text)
  1. import webbrowser
  2. # html code to dislay two animated gifs next to each other
  3. str1 = """
  4. <html>
  5. <head>
  6. <title>Animated HI</title>
  7. </head>
  8. <body>
  9.  
  10. <img src="Ag_H.gif" width=170 height=170 border=0>
  11. <img src="Ag_I.gif" width=170 height=170 border=0>
  12.  
  13. </body>
  14. </html>"""
  15.  
  16. # write the html file to the working folder
  17. fout = open("Animated_HI.htm", "w")
  18. fout.write(str1)
  19. fout.close()
  20.  
  21. # now open your web browser to run the file
  22. webbrowser.open("Animated_HI.htm")

Want to do even more? No problem, how about showing three pictures with overlap:
python Syntax (Toggle Plain Text)
  1. import webbrowser
  2.  
  3. # html code to overlap three pictures
  4. # if you supply your own pictures, you may have to change
  5. # widths, heights and absolute positions
  6. str1 = """
  7. <html>
  8. <head>
  9. <title>Overlaping Images</title>
  10. </head>
  11. <body bgcolor="#66CC33">
  12. <!-- you may need to supply your own image files -->
  13. <!-- and adjust their widths and heights accordingly -->
  14. <div style="position:absolute; left:250; top:420; width:360; height:245">
  15. <img id = "image1" width = 360 height = 245 src = "Benz88L.jpg">
  16. </div>
  17.  
  18. <div style="position:absolute; left:150; top:220; width:360; height:245">
  19. <img id = "image2" width = 360 height = 245 src = "Benz84L.jpg">
  20. </div>
  21.  
  22. <div style="position:absolute; left:50; top:20; width:360; height:245">
  23. <img id = "image3" width = 360 height = 245 src = "Benz83L.jpg">
  24. </div>
  25.  
  26. </body>
  27. </html>"""
  28.  
  29. # write the html file to the working folder
  30. fout = open("ImageOverlap.htm", "w")
  31. fout.write(str1)
  32. fout.close()
  33.  
  34. # now open your web browser to run the file
  35. webbrowser.open("ImageOverlap.htm")
  36.  

Amazingly, webbrowser is not finished yet, because browsers also like MIDI files that play sound effects and instrumental background music. The small size of the .mid file means that it can be transmitted quickly on the Internet. You could use webbrowser.open("MyDrums.mid") and it would find a default player, but you can also embed the file within HTML code like this:
python Syntax (Toggle Plain Text)
  1. import webbrowser
  2.  
  3. # html code to embed a midi music file for background music
  4. str1 = """
  5. <html>
  6. <head>
  7. <title>Embedded MIDI Player</title>
  8. </head>
  9. <body text="#FF66CC" onLoad="resizeTo(460,320)">
  10. <h4>The midi file will auto start!</h4>
  11. <!-- you can put your own midi file here -->
  12. <embed src = "Drum1.mid" width="400" height="20"
  13. hidden=false autostart=true loop=1>
  14. </body>
  15. </html>"""
  16.  
  17. # write the html file to the working folder
  18. fout = open("EmbeddedMidi.htm", "w")
  19. fout.write(str1)
  20. fout.close()
  21.  
  22. # now open your web browser to run the file
  23. webbrowser.open("EmbeddedMidi.htm")
  24.  

You can even play movies in the MPG, MOV, AVI, SWF formats just by modifying the above HTML code string very slightly as follows:
python Syntax (Toggle Plain Text)
  1. str1 = """
  2. <html>
  3. <head>
  4. <title>Embedded MPG Player</title>
  5. </head>
  6. <body>
  7. <!-- put your own .mpg movie file here -->
  8. <!-- adjust width and height to your needs -->
  9. <embed src = "Whateveryouhave.mpg" width="400" height="350"
  10. hidden=false autostart=true loop=1>
  11. </body>
  12. </html>
  13. """

And finally, for those amongst you that don't like cluttering your drives with temporary files, there is even the option of creating a temporary http server specifically to handle the webbrowser request, using the following code:
python Syntax (Toggle Plain Text)
  1. # display html in the default web browser without creating a temp file
  2. # does not write needed .bmp and .wav files to the temporary http server,
  3. # so you have to give full pathnames for these files!
  4.  
  5. import BaseHTTPServer as bhs
  6. import webbrowser
  7.  
  8. def load_default_browser(html):
  9. class RequestHandler(bhs.BaseHTTPRequestHandler):
  10. def do_GET(self):
  11. bufferSize = 1024*1024
  12. for i in xrange(0, len(html), bufferSize):
  13. self.wfile.write(html[i:i+bufferSize])
  14.  
  15. # create a temporary http server to handle one request
  16. server = bhs.HTTPServer(('127.0.0.1', 0), RequestHandler)
  17. # calls URL to retrieve html from the temporary http server
  18. webbrowser.open('http://127.0.0.1:%s' % server.server_port)
  19. server.handle_request()
  20.  
  21. html = """<html>
  22. <head>
  23. <title>image with sound</title>
  24. </head>
  25. <body>
  26. <!-- give full path for your picture and sound files -->
  27. <IMG SRC="D:/Python24/Atest/train.bmp" WIDTH=320 HEIGHT=210 BORDER=5>
  28. <bgsound src="D:/Python24/Atest/train.wav" loop=2>
  29. </body>
  30. </html>
  31. """
  32.  
  33. load_default_browser(html)


All the multimedia files used in this tutorial can be found in the attached Multimedia.zip file
Attached Files
File Type: zip Multimedia.zip (295.2 KB, 157 views)
Last edited by happygeek; Feb 27th, 2007 at 1:03 pm.
Similar Threads
Comments on this Tutorial
Jan 13th, 2008
0

Re: Multimedia: introducing the webbrowser module

For some odd reason it seems that the zip file containing the multimedia test files got lost. Well here it is ...
Attached Files
File Type: zip Multimedia.zip (295.2 KB, 54 views)
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Jan 18th, 2008
0

Re: Multimedia: introducing the webbrowser module

That webbrowser module in Python works fine on Debian Linux! I just opened a basic HTML page in lynx. So it even works on Linux with no GUI!
Newbie Poster
wwwalker is offline Offline
3 posts
since Oct 2007
Mar 19th, 2008
0

Re: Multimedia: introducing the webbrowser module

It uses firefox to play mp3's. Odd.
But overall 10/10.
Light Poster
1337455 10534 is offline Offline
42 posts
since Oct 2007
Aug 24th, 2009
0

Re: Multimedia: introducing the webbrowser module

Im really impressed, it works perfectly on windows vista, nice work.
5 Stars.
Junior Poster
Dan08 is offline Offline
133 posts
since Jul 2009
Apr 23rd, 2011
0

Re: Multimedia: introducing the webbrowser module

I think I'll learn a lot from this thanks.
Posting Pro in Training
e-papa is offline Offline
462 posts
since Mar 2011
Message:
Previous Thread in Python Forum Timeline: python sounds
Next Thread in Python Forum Timeline: Useful input trick





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC