JPEG Image Embedded in Python

vegaseat 2 Tallied Votes 4K Views Share

Small image files can be embedded in Python code using the base64 encoded string of the image. If you have to present a number of small fixed images in your program, embedding might be easier than including a set of image files. Attached files can be lost, and than your program won't work. The snippet uses the ever popular jpeg image and wxPython to display the image. I have used a panel here, but you could also use a button or a label.

For your sanity, you do not type the image string, you let a small Python program create it, then copy and paste it into your code. For an example how to do this see:
http://www.daniweb.com/code/snippet394.html

# show an embedded base64 encoded jpeg (.jpg) image using wxPython
# the base 64 encoded image string was created with (then copied and pasted):
#   import base64
#   jpgfile = "halloween3.jpg"
#   jpg_text = 'jpg1_b64 = \\\n"""' + base64.encodestring(open(jpgfile,"rb").read()) + '"""'
#   print jpg_text
# tested with Python24 and wxPython25   vegaseat     06oct2005

import wx
import cStringIO
import base64

jpg1_b64 = \
"""/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAsICAoIBwsKCQoNDAsNERwSEQ8PESIZGhQcKSQrKigk
JyctMkA3LTA9MCcnOEw5PUNFSElIKzZPVU5GVEBHSEX/2wBDAQwNDREPESESEiFFLicuRUVFRUVF
RUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUX/wAARCABVAGADASIA
AhEBAxEB/8QAHAAAAQQDAQAAAAAAAAAAAAAABwAEBQYCAwgB/8QAPRAAAgECBAQCBwUGBgMAAAAA
AQIDBBEABRIhBhMxQSJRBxQyYXGBkSNCUqGzFTM2dIKyFzRyosHwQ5Kx/8QAGgEAAQUBAAAAAAAA
AAAAAAAAAwACBAUGAf/EADARAAIBAgMECQMFAAAAAAAAAAECAAMRBBIhBTFh8AYTIiNBUYGhwRSx
0TNxkeHx/9oADAMBAAIRAxEAPwAuYhuKuJKbhbJZK+oUu5PLhiA/eSEEgX7DYknyHc2BmcBj0yVE
snEmX0jSDkR0okVSNlZnYMb9eiL9MSMNSFWqFO6cY2Epuc57mnE+Y86umknkdyIoUvojvYaUXt0A
8zbe5xqosqlragRQIZWC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tdvLFq53PjQc1qmG9ubBO5f5iJbfng4ULoBOTdGHalRY9a6NiNDLf4ex/8xmrAsASA67FWIv8A
Hufzxg4EciO6ow6KWQBx5bs17/LEjldFJmFfFTIzOrEM7beFO52H/dsJmCgsd0UJWFhYWMpDwecS
+ijKq4vV5ZKctcBneNU1xt1Oy3Gn5G1hsMCHNKD9m1r0/M5mm/i026Ej/jCwsXWz6rvdWN7QbgCM
8LCwsWsHLXl2YLTUsDBJOYYgCymNbgCw8QTX2/FiTlknjpTNM4liIBMbPK3W343Zf9uFhYdkXSKa
8vqy+uKkRYbi5LKo+nLEZ/PDpi3J5VQxk3vcEv8Aq68LCx3It4pbMi4aGYUUbettHGApKiMA2IBs
CtgPpi5ZfllNlkJjpkILW1uxuznzJ/46b4WFjNYus7OUJ0hlAtP/2Q==
"""

jpg1 = base64.b64decode(jpg1_b64)

class Panel1(wx.Panel):
  """ class Panel1 creates a panel with an image on it, inherits wx.Panel """
  def __init__(self, parent, id):
    # create the panel
    wx.Panel.__init__(self, parent, id)
    # convert jpg stream to a data stream
    stream1 = cStringIO.StringIO(jpg1)
    # convert to a bitmap
    bmp1 = wx.BitmapFromImage(wx.ImageFromStream(stream1))
    # show the bitmap, (5, 5) are upper left corner coordinates
    wx.StaticBitmap(self, -1, bmp1, (5, 5))


app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
# increase the size of the frame for larger images
frame1 = wx.Frame(None, -1, "An image on a panel", size = (400, 300))
# call the derived class
Panel1(frame1, -1)
frame1.Show(1)
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.