| | |
JPEG Base64 Image String (Python)
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
This short code lets you create a base64 encoded image string, that you can copy and paste into your Python program code to display relatively small embedded images.
For a typical application see:
http://www.daniweb.com/code/snippet393.html
For a typical application see:
http://www.daniweb.com/code/snippet393.html
# base64 encoding converts binary data to plain text # it stores each group of three binary bytes as a group of four characters from the text set: # ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789+/ # the = character is used for padding at the end of the data stream # this way an image file can be represented as a string to transmit or # a short image can be embedded in code as a string, you can insert newlines at # convenient locations into this embedded string, they will be ignored when # decoding with for instance jpg1 = base64.b64decode(jpg1_b64) # tested with Python24 vegaseat 06oct2005 import base64 # pick a jpeg file you have and want ... jpgfile = "halloween3.jpg" # note: binary read "rb" is required! # this gives a one line string ... #jpg_text = 'jpg1_b64 = \\\n"""' + base64.b64encode(open(jpgfile,"rb").read()) + '"""' # another option, inserts newlines about every 76 characters, easier to copy and paste! jpg_text = 'jpg1_b64 = \\\n"""' + base64.encodestring(open(jpgfile,"rb").read()) + '"""' print jpg_text # optionally save to a text file filename = "jpg1_b64.txt" try: fout = open(filename, "w") fout.write(jpg_text) fout.close() except IOError: print "File %s could not be saved!" % filename
Similar Threads
- encode-decode to base64 string (C#)
- converting JPEG colour image into gray scale image?? (Java)
- Python and the JPEG Image File, Part 2, The Image (Python)
- Code Snippet: JPEG Image Embedded in Python (Python)
- Python and the JPEG Image File, Part 1, The Header (Python)
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array backend beginner binary book builtin calculator character chmod converter countpasswordentry curved dan08 dictionaries dictionary drive dynamic examples excel exe file filename float format function graphics gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython



