Welcome, guest | Sign In | My Account | Store | Cart

Say you want to generate a default image with my app without having to include an image file with my app. You'll want to encode the binary image data as text, using base64. This image can later be displayed in my app, or a binary image file can be written by decoding the encoded text string using the base64 module. To make this task a little easier for you, I created a small GUI app using Wax. Simply run the app, select the image file you want converted, then copy the encoded text string and paste it into your app (which could be a Wax, wxPython, Qt or even Tkinter app). The last part of the recipe shows you how to decode that text string back to binary data.

Python, 56 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# imageviewer.py
#requires python 2.x

from wax import *
import base64
import cStringIO
import os
import sys
 
class MainFrame(VerticalFrame):
        
    def Body(self):
        self.SetTitle("Image Viewer")
        whereami = os.path.abspath(os.path.dirname(__file__))
        os.chdir(whereami)
        wildcard="GIF images|*.gif|JPG images|*.jpg|JPEG images|*.jpeg|PNG images|*.png"
        dlg = FileDialog(self, title="Select Image file to display", 
                wildcard=wildcard, open=1, multiple=0)
        if dlg.ShowModal() == 'ok':
            filename = dlg.GetPath()
        else:
            raise SystemExit
        dlg.Destroy()
        label1 = Label(self, "Image as image...")
        self.AddComponent(label1, expand='h', border=5)
        label1.BackgroundColor = self.BackgroundColor = 'white'
        bitmap = Image(filename).ConvertToBitmap()
        self.AddComponent(Bitmap(self, bitmap))


        label2 = Label(self, "Image encoded as text...")
        self.AddComponent(label2, expand='h', border=5)
        label2.BackgroundColor = self.BackgroundColor = 'white'
        imageEncodeText = base64.encodestring(open(filename, "rb").read()) 
        imageText = TextBox(self, multiline=1, readonly=1,
                       Font=Font("Courier New", 8), Size=(650,200),
                       Value=imageEncodeText)
        self.AddComponent(imageText, expand='both')

        label2 = Label(self, "Text decoded back to image...")
        self.AddComponent(label2, expand='h', border=5)
        label2.BackgroundColor = self.BackgroundColor = 'white'
        data = base64.decodestring(imageEncodeText)
        path,fid = os.path.split(filename)
        head,tail = os.path.split(fid)
        fileout = "%s%sout%s" % (path,head,tail)
        f1 = open(fileout,'wb+')
        f1.write(data)
        f1.close()
        bitmap2 = Image(fileout).ConvertToBitmap()
        self.AddComponent(Bitmap(self, bitmap2))

        self.Pack()

app = Application(MainFrame)
app.MainLoop()

This recipe isn't rocket science, but could help a newbie. I chose to create this recipe in Wax, a pythonic wrapper around wxPython, available from http://zephyrfalcon.org/labs/wax.html, since I'm using it for a Python off-line blogging app, Firedrop2 - itself available from http://www.voidspace.org.uk/python/firedrop2/ .

Acknowledgements: thanks to Brent Burley for his recipe "Inline GIFs with Tkinter", http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52264, and to Hans Nowak for Wax, which greatly smooths out the wxPython bumps.