Background Image file

Hello everyone,

I am trying to build a .exe app using pyinstaller to make it standalone.

I have an issue with the background image, I am using wxPython for the gui.

My background image file is not in the .exe, it has to be in the folder where my .exe is else my program is not working.

Every other dll, icon file, whatever else my program needs is in the standalone .exe.

If anyone would know how to integrate my image file to make sure this is a standalone .exe this would be perfect.

The important part of my code for this is the following:

class MainPanel(wx.Panel):
        """"""

        def OnClose(self, e):
                parse.parsing()
        def __init__(self, parent):
                """Constructor"""
                wx.Panel.__init__(self, parent=parent)
                #self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
                self.frame = parent

                sizer = wx.BoxSizer(wx.VERTICAL)
                hSizer = wx.BoxSizer(wx.HORIZONTAL)

                cbtn = wx.Button(self, label='Configure', pos=(196, 144))
                cbtn.Bind(wx.EVT_BUTTON, self.OnClose)

                self.SetSizer(hSizer)
                self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

        def OnEraseBackground(self, evt):
                """
                Background image
                """
        # yanked from ColourDB.py
                dc = evt.GetDC()

                if not dc:
                        dc = wx.ClientDC(self)
                        rect = self.GetUpdateRegion().GetBox()
                        dc.SetClippingRect(rect)
                dc.Clear()
                bmp = wx.Bitmap("pf_bg.png")
                dc.DrawBitmap(bmp, 0, 0)

For information when compiling with pyinstaller I use the following:
python pyinstaller.py --version-file=version.txt -F --noconsole --icon=ic.ico -n blabla gui.py

When I try to open my .exe without my image file in the folder I have the error in the attached file.

I am not sure if I have to fix something with wx or pyinstaller.

Everything is compile on win7(64) with python 2.6.6

Thanks,

Antoine

error.png

encode it as base64 and paste the resultant string into a variable somewhere, then load that into an image and convert to a bitmap.

I’ve attached some example code, which I improved based on the following link, to get rid of the intermediate file they were using:

wallpaper/tile a wxPython panel using a base 64 encoded image string

“”"

the image string is created with this simple Python code

and the string is then copied and pasted to the program

import base64

jpg_file = “BG_Egypt.jpg”

print \

“jpg_b64=‘’'\\n” + base64.encodestring(open(jpg_file,“rb”).read()) + “‘’'”

“”"

import wx

import base64

import StringIO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ZLm3yV0t4fl85C2X8sYG3OeeQS

O/N3xzzBbd/3une/8cP1/wA/qbWab36gHjnmC27/AL3Tvf8Ajh+v+f1PHPMFt3/e6d7/AMcP1/z+

uXr2sDV1hRIb553urU86fLGoVJYz3QKAFH+e+hresafcXapJDfSiHyDxYTsvmRhCOQhDAMo9QfcH

k1te3W4Fu98UW0E/kQW8xWNEQkaQ7gsFAb5vLO75ieckHtnvia9rA1dYUSG+ed7q1POnyxqFSWM9

0CgBR/nvqf8ACWxn/l2n5/6gj/8Axr3/AM9z/hLYz/y7T8/9QR//AI17/wCe58mB/9k=

‘’’

jpg1 = base64.b64decode(BG_Egypt_jpg_b64)

class Panel1(wx.Panel):

""" 

class Panel1 creates a panel for the tile image

fw and fh are the width and height of the base frame

"""

def __init__(self, parent, id, fw, fh, image_data):

    # create the panel

    wx.Panel.__init__(self, parent, id)

    # frame/panel width and height

    self.fw = fw

    self.fh = fh

    # load the wallpaper/tile file

    sbuf = StringIO.StringIO(image_data)

    image = wx.ImageFromStream(sbuf)

    self.bmp1 = wx.BitmapFromImage(image)

    # do the wall papering ...

    wx.EVT_PAINT(self, self.on_paint)

    # now put a button on the panel, on top of wallpaper

    self.button1 = wx.Button(self, -1, label='Button1', pos=(10, 5))        

    

def on_paint(self, event=None):

    # create paint surface

    dc = wx.PaintDC(self)

    dc.Clear()

    #dc.SetBackgroundMode(wx.TRANSPARENT)

    # get image width and height

    iw = self.bmp1.GetWidth()

    ih = self.bmp1.GetHeight()

    # tile/wallpaper the image across the canvas

    for x in range(0, self.fw, iw):

        for y in range(0, self.fh, ih):

            dc.DrawBitmap(self.bmp1, x, y, True)

app = wx.App()

create a window/frame instance, no parent, -1 is default ID

fw = 400

fh = 300

frame1 = wx.Frame(None, -1, “wall-papering a wx.PaintDC surface”, size=(fw, fh))

create a panel class instance

panel1 = Panel1(frame1, -1, fw, fh, image_data = jpg1)

frame1.Show(True)

start the event loop

app.MainLoop()

testbase64.py (3.27 KB)

···

On Friday, November 14, 2014 12:16:38 PM UTC-8, Antoine Amacher wrote:

If anyone would know how to integrate my image file to make sure this is a standalone .exe this would be perfect.

encode it as base64 and paste the resultant string into a variable
somewhere, then load that into an image and convert to a bitmap.

wxPython ships with utilities to do somethign much like this -- lok for teh
img2py script.

And it is, indeed, really handy particularly for bundling up with py2exe
and the like.

-Chris

···

On Fri, Nov 14, 2014 at 12:46 PM, Nathan McCorkle <nmz787@gmail.com> wrote:

I've attached some example code, which I improved based on the following
link, to get rid of the intermediate file they were using:

Wallpapering a wxPython background | DaniWeb

# wallpaper/tile a wxPython panel using a base 64 encoded image string
"""
# the image string is created with this simple Python code
# and the string is then copied and pasted to the program
import base64
jpg_file = "BG_Egypt.jpg"
print \
"jpg_b64='''\\\n" + base64.encodestring(open(jpg_file,"rb").read()) + "'''"

"""

import wx
import base64
import StringIO

BG_Egypt_jpg_b64='''\

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU

FhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo

KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIA

AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA

AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3

ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm

p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA

AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx

BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK

U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3

uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwB2taqm

lRI7pAsCxQDi0WVizqo7KzEln9+v55f/AAlsZ/5dp+f+oI//AMa9/wDPc8c8wW3f97p3v/HD9f8A

P66mtaqmlRI7pAsCxQDi0WVizqo7KzEln9+v5+J9+9tDQy/+EtjP/LtPz/1BH/8AjXv/AJ7x67q0

9xo8Lxp5AlurUc2ggk2mdAeCoYAgn6g9wea2r+Inv7aK3tILpZZLm3yV0t4fl85C2X8sYG3OeeQS

O/N3xzzBbd/3une/8cP1/wA/qbWab36gHjnmC27/AL3Tvf8Ajh+v+f1PHPMFt3/e6d7/AMcP1/z+

uXr2sDV1hRIb553urU86fLGoVJYz3QKAFH+e+hresafcXapJDfSiHyDxYTsvmRhCOQhDAMo9QfcH

k1te3W4Fu98UW0E/kQW8xWNEQkaQ7gsFAb5vLO75ieckHtnvia9rA1dYUSG+ed7q1POnyxqFSWM9
0CgBR/nvqf8ACWxn/l2n5/6gj/8Axr3/AM9z/hLYz/y7T8/9QR//AI17/wCe58mB/9k=
'''

jpg1 = base64.b64decode(BG_Egypt_jpg_b64)

class Panel1(wx.Panel):
    """
    class Panel1 creates a panel for the tile image
    fw and fh are the width and height of the base frame
    """
    def __init__(self, parent, id, fw, fh, image_data):
        # create the panel
        wx.Panel.__init__(self, parent, id)
        # frame/panel width and height
        self.fw = fw
        self.fh = fh
        # load the wallpaper/tile file
        sbuf = StringIO.StringIO(image_data)

        image = wx.ImageFromStream(sbuf)
        self.bmp1 = wx.BitmapFromImage(image)
        # do the wall papering ...
        wx.EVT_PAINT(self, self.on_paint)
        # now put a button on the panel, on top of wallpaper
        self.button1 = wx.Button(self, -1, label='Button1', pos=(10, 5))

    def on_paint(self, event=None):
        # create paint surface
        dc = wx.PaintDC(self)
        dc.Clear()
        #dc.SetBackgroundMode(wx.TRANSPARENT)
        # get image width and height
        iw = self.bmp1.GetWidth()
        ih = self.bmp1.GetHeight()
        # tile/wallpaper the image across the canvas
        for x in range(0, self.fw, iw):
            for y in range(0, self.fh, ih):
                dc.DrawBitmap(self.bmp1, x, y, True)

app = wx.App()
# create a window/frame instance, no parent, -1 is default ID
fw = 400
fh = 300
frame1 = wx.Frame(None, -1, "wall-papering a wx.PaintDC surface",
size=(fw, fh))
# create a panel class instance
panel1 = Panel1(frame1, -1, fw, fh, image_data = jpg1)
frame1.Show(True)
# start the event loop
app.MainLoop()

--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Hello,

I managed to archive what I was looking for with img2py.

Thanks for the help

···

Le vendredi 14 novembre 2014 15:16:38 UTC-5, Antoine Amacher a écrit :

Hello everyone,

I am trying to build a .exe app using pyinstaller to make it standalone.

I have an issue with the background image, I am using wxPython for the gui.

My background image file is not in the .exe, it has to be in the folder where my .exe is else my program is not working.

Every other dll, icon file, whatever else my program needs is in the standalone .exe.

If anyone would know how to integrate my image file to make sure this is a standalone .exe this would be perfect.

The important part of my code for this is the following:

class MainPanel(wx.Panel):
        """"""

        def OnClose(self, e):
                parse.parsing()
        def __init__(self, parent):
                """Constructor"""
                wx.Panel.__init__(self, parent=parent)
                #self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
                self.frame = parent

                sizer = wx.BoxSizer(wx.VERTICAL)
                hSizer = wx.BoxSizer(wx.HORIZONTAL)

                cbtn = wx.Button(self, label='Configure', pos=(196, 144))
                cbtn.Bind(wx.EVT_BUTTON, self.OnClose)

                self.SetSizer(hSizer)
                self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

        def OnEraseBackground(self, evt):
                """
                Background image
                """
        # yanked from ColourDB.py
                dc = evt.GetDC()

                if not dc:
                        dc = wx.ClientDC(self)
                        rect = self.GetUpdateRegion().GetBox()
                        dc.SetClippingRect(rect)
                dc.Clear()
                bmp = wx.Bitmap("pf_bg.png")
                dc.DrawBitmap(bmp, 0, 0)

For information when compiling with pyinstaller I use the following:
python pyinstaller.py --version-file=version.txt -F --noconsole --icon=ic.ico -n blabla gui.py

When I try to open my .exe without my image file in the folder I have the error in the attached file.

I am not sure if I have to fix something with wx or pyinstaller.

Everything is compile on win7(64) with python 2.6.6

Thanks,

Antoine