| 1 |
"""Provides the `PyEmbeddedImage` class for embedding PNG image data in Python code. |
| 2 |
|
| 3 |
It is primarily used with the `wx.tools.img2py` code generator to simplify the |
| 4 |
distribution of image assets by embedding them as base64-encoded data within |
| 5 |
Python modules. |
| 6 |
""" |
| 7 |
# ---------------------------------------------------------------------- |
| 8 |
# Name: wx.lib.embeddedimage |
| 9 |
# Purpose: Defines a class used for embedding PNG images in Python |
| 10 |
# code. The primary method of using this module is via |
| 11 |
# the code generator in wx.tools.img2py. |
| 12 |
# |
| 13 |
# Author: Anthony Tuininga |
| 14 |
# |
| 15 |
# Created: 26-Nov-2007 |
| 16 |
# RCS-ID: $Id: embeddedimage.py 59672 2009-03-20 20:59:42Z RD $ |
| 17 |
# Copyright: (c) 2007 by Anthony Tuininga |
| 18 |
# Licence: wxWindows license |
| 19 |
# ---------------------------------------------------------------------- |
| 20 |
|
| 21 |
import base64 |
| 22 |
import io |
| 23 |
|
| 24 |
import wx |
| 25 |
|
| 26 |
try: |
| 27 |
b64decode = base64.b64decode |
| 28 |
except AttributeError: |
| 29 |
b64decode = base64.decodestring |
| 30 |
|
| 31 |
|
| 32 |
class PyEmbeddedImage: |
| 33 |
"""A class for embedding PNG images in Python code. |
| 34 |
|
| 35 |
PyEmbeddedImage is primarily intended to be used by code generated |
| 36 |
by img2py as a means of embedding image data in a python module so |
| 37 |
the image can be used at runtime without needing to access the |
| 38 |
image from an image file. This makes distributing icons and such |
| 39 |
that an application uses simpler since tools like py2exe will |
| 40 |
automatically bundle modules that are imported, and the |
| 41 |
application doesn't have to worry about how to locate the image |
| 42 |
files on the user's filesystem. |
| 43 |
|
| 44 |
The class can also be used for image data that may be acquired |
| 45 |
from some other source at runtime, such as over the network or |
| 46 |
from a database. In this case pass False for isBase64 (unless the |
| 47 |
data actually is base64 encoded.) Any image type that |
| 48 |
wx.ImageFromStream can handle should be okay. |
| 49 |
|
| 50 |
Args: |
| 51 |
data (bytes): The image data, which can be base64 encoded or raw binary |
| 52 |
data. |
| 53 |
is_base64 (bool): If True, the `data` is expected to be base64 encoded. |
| 54 |
If False, the `data` is treated as raw binary data. Defaults to |
| 55 |
True. |
| 56 |
""" |
| 57 |
|
| 58 |
def __init__(self, data: bytes, is_base64: bool = True) -> None: |
| 59 |
self.data = data |
| 60 |
self.is_base64 = is_base64 |
| 61 |
|
| 62 |
def GetBitmap(self) -> wx.Bitmap: # noqa: N802 |
| 63 |
"""Return a wx.Bitmap object created from the embedded image data. |
| 64 |
|
| 65 |
Returns: |
| 66 |
wx.Bitmap: The bitmap created from the embedded data. |
| 67 |
""" |
| 68 |
return wx.BitmapFromImage(self.GetImage()) |
| 69 |
|
| 70 |
def GetData(self) -> bytes: # noqa: N802 |
| 71 |
"""Return the raw image data, decoding it if necessary. |
| 72 |
|
| 73 |
Returns: |
| 74 |
bytes: The raw image data, decoded if it was base64 encoded. |
| 75 |
""" |
| 76 |
if self.is_base64: |
| 77 |
data = b64decode(self.data) |
| 78 |
# TODO: what is ``data`` if self.isBase64 is False |
| 79 |
return data |
| 80 |
|
| 81 |
def GetIcon(self) -> wx.Icon: # noqa: N802 |
| 82 |
"""Return a wx.Icon object created from the embedded image data. |
| 83 |
|
| 84 |
Returns: |
| 85 |
wx.Icon: The icon created from the embedded data. |
| 86 |
""" |
| 87 |
icon = wx.EmptyIcon() |
| 88 |
icon.CopyFromBitmap(self.GetBitmap()) |
| 89 |
return icon |
| 90 |
|
| 91 |
def GetImage(self) -> wx.Image: # noqa: N802 |
| 92 |
"""Return a wx.Image object created from the embedded image data. |
| 93 |
|
| 94 |
Returns: |
| 95 |
wx.Image: The image created from the embedded data. |
| 96 |
""" |
| 97 |
stream = io.BytesIO(self.GetData()) |
| 98 |
return wx.ImageFromStream(stream) |
| 99 |
|
| 100 |
# define properties, for convenience |
| 101 |
Bitmap = property(GetBitmap) |
| 102 |
Icon = property(GetIcon) |
| 103 |
Image = property(GetImage) |