constructing a blank bitmap?

1,136 views
Skip to first unread message

Nat Echols

unread,
Jun 29, 2010, 7:49:58 PM6/29/10
to wxpython-users
Is there an easy way to do this in wxPython?  I want to use a blank image for certain items in a ListCtrl; currently I just use a 16x16 blank PNG that I made, but for portability reasons I'd like to do this internally if possible.  Alternately, if I'm just wasting time and there's an easier way to make the image in a ListCtrl blank, that would work too - but if I don't use any image, it messes up the column alignment.

thanks,
Nat

C M

unread,
Jun 29, 2010, 9:43:10 PM6/29/10
to wxpytho...@googlegroups.com
On Tue, Jun 29, 2010 at 7:49 PM, Nat Echols <nathanie...@gmail.com> wrote:
> Is there an easy way to do this in wxPython?  I want to use a blank image
> for certain items in a ListCtrl;

How about wx.NullBitmap?

> currently I just use a 16x16 blank PNG that
> I made, but for portability reasons I'd like to do this internally if
> possible.

Although here wx.NullBitmap is a better approach in this case, for the
future you might want to know that you can use img2py to make any
image you want into pure Python code and so include images without
having to include the image files.

Che

monobot

unread,
Jun 30, 2010, 4:33:07 AM6/30/10
to wxPython-users
This is an example from the wxpython demo

def GetExpandedIconData():
return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR
\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d
\x88\x00\
\x00\x01\x9fIDAT8\x8d\x95\x93\xa1\x8e\xdc0\x14EO
\xb2\xc4\xd0\xd2\x12\xb7(mI\
\xa4%V\xd1lQT4[4-\x9a\xfe\xc1\xc2|\xc6\xc2~BY\x83:A3E\xd3\xa0*
\xa4\xd2\x90H!\
\x95\x0c\r\r\x1fK\x81g\xb2\x99\x84\xb4\x0fY\xd6\xbb\xc7\xf7>=\'Iz
\xc3\xbcv\
\xfbn\xb8\x9c\x15 \xe7\xf3\xc7\x0fw\xc9\xbc7\x99\x03\x0e\xfbn0\x99F+
\x85R\
\x80RH\x10\x82\x08\xde\x05\x1ef\x90+\xc0\xe1\xd8\ryn\xd0Z-\\A
\xb4\xd2\xf7\
\x9e\xfbwoF\xc8\x088\x1c\xbbae\xb3\xe8y&\x9a\xdf\xf5\xbd\xe7\xfem
\x84\xa4\
\x97\xccYf\x16\x8d\xdb\xb2a]\xfeX\x18\xc9s\xc3\xe1\x18\xe7\x94\x12cb
\xcc\xb5\
\xfa\xb1l8\xf5\x01\xe7\x84\xc7\xb2Y@\xb2\xcc0\x02\xb4\x9a\x88%\xbe\xdc
\xb4\
\x9e\xb6Zs\xaa74\xadg[6\x88<\xb7]\xc6\x14\x1dL\x86\xe6\x83\xa0\x81\xba
\xda\
\x10\x02x/\xd4\xd5\x06\r\x840!\x9c\x1fM\x92\xf4\x86\x9f\xbf\xfe\x0c
\xd6\x9ae\
\xd6u\x8d \xf4\xf5\x165\x9b\x8f\x04\xe1\xc5\xcb\xdb$\x05\x90\xa97@
\x04lQas\
\xcd*7\x14\xdb\x9aY\xcb\xb8\\\xe9E\x10|\xbc\xf2^\xb0E\x85\xc95_\x9f\n
\xaa/\
\x05\x10\x81\xce\xc9\xa8\xf6><G\xd8\xed\xbbA)X\xd9\x0c\x01\x9a\xc6Q
\x14\xd9h\
[\x04\xda\xd6c\xadFkE\xf0\xc2\xab\xd7\xb7\xc9\x08\x00\xf8\xf6\xbd\x1b
\x8cQ\
\xd8|\xb9\x0f\xd3\x9a\x8a\xc7\x08\x00\x9f?\xdd%\xde\x07\xda
\x93\xc3{\x19C\
\x8a\x9c\x03\x0b8\x17\xe8\x9d\xbf\x02.>\x13\xc0n\xff{PJ\xc5\xfdP\x11""<
\xbc\
\xff\x87\xdf\xf8\xbf\xf5\x17FF\xaf\x8f\x8b\xd3\xe6K\x00\x00\x00\x00IEND
\xaeB\
`\x82'

def GetExpandedIconBitmap():
return wx.BitmapFromImage(GetExpandedIconImage())

def GetExpandedIconImage():
import cStringIO
stream = cStringIO.StringIO(GetExpandedIconData())
return wx.ImageFromStream(stream)

Im sure you can find a sollution with it.

Mike Driscoll

unread,
Jun 30, 2010, 10:03:01 AM6/30/10
to wxPython-users
I use wx.EmptyImage for my blank images. You just pass it a width and
height and it creates a black square. The others have interesting
ideas too. Alternatively, you could take the image you are using and
run it through img2py to make the bitmap into a python file.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

WinCrazy

unread,
Jun 30, 2010, 12:49:20 PM6/30/10
to wxPython-users
Very few people want to display a black bitmap (duh!), which is the
default bitmap creation color.
Try something like this:

# Get the parent's container (a ListCtrl ?) dimensions.
clientSize = self.GetClientSizeTuple()

# Create a new (black!) bitmap that size.
self.wxBitmap = wx.EmptyBitmap( *clientSize )

# Re-color the bitmap to !your! choice.
# DC's are used to manipulate and/or display wx.Bitmaps.
dc = wx.MemoryDC( self.wxBitmap ) # a temporary DC
# Get the color you want. This is just an example.
myColor = self.GetBackgroundColour()
dc.SetBackground( wx.Brush( myColor ) )
dc.Clear()
# "Close" the DC - make is unavailable for future use.
dc.SelectObject( wx.NullBitmap )

Note that in wx, someone forgot to create an
Americanized function "GetBackgroundColor()".
Note the Brit spelling. The majority of wx methods
and argument names with the word "colour" in it
also have Americanized spelling equivalents.

Ray Pasco

Robin Dunn

unread,
Jun 30, 2010, 1:02:12 PM6/30/10
to wxpytho...@googlegroups.com
On 6/29/10 6:43 PM, C M wrote:
> On Tue, Jun 29, 2010 at 7:49 PM, Nat Echols<nathanie...@gmail.com> wrote:
>> Is there an easy way to do this in wxPython? I want to use a blank image
>> for certain items in a ListCtrl;
>
> How about wx.NullBitmap?
>

wx.NullBitmap is not an empty bitmap, it is an invalid one used for
things like what we would normally use None for in Python.

>>> import wx
>>> wx.NullBitmap.Ok()
False
>>> wx.EmptyBitmap(16,16).Ok()
True

--
Robin Dunn
Software Craftsman
http://wxPython.org

Robin Dunn

unread,
Jun 30, 2010, 1:03:00 PM6/30/10
to wxpytho...@googlegroups.com
On 6/30/10 7:03 AM, Mike Driscoll wrote:
>
>
> On Jun 29, 6:49 pm, Nat Echols<nathaniel.ech...@gmail.com> wrote:
>> Is there an easy way to do this in wxPython? I want to use a blank image
>> for certain items in a ListCtrl; currently I just use a 16x16 blank PNG that
>> I made, but for portability reasons I'd like to do this internally if
>> possible. Alternately, if I'm just wasting time and there's an easier way
>> to make the image in a ListCtrl blank, that would work too - but if I don't
>> use any image, it messes up the column alignment.
>>
>> thanks,
>> Nat
>
>
> I use wx.EmptyImage for my blank images. You just pass it a width and
> height and it creates a black square.

It happens to be that way on Windows because of the way the native API
works, but be aware that on other platforms the RGB data for a bitmap
created this way will just use whatever happens to be in that memory at
the time, IOW, random junk. So you should not depend on the initial
content being black and if you want it to be a certain color you can
initialize it yourself like this:

bmp = wx.EmptyBitmap(w, h)
dc = wx.MemoryDC(bmp)
dc.SetBackground(wx.Brush("black")) # or some other color
dc.Clear()
del dc

If you also want the bitmap to be transparent (which you probably do in
this case) then you can set the mask using the color that the bitmap was
Clear()ed to:

bmp.SetMaskColour('black')

Reply all
Reply to author
Forward
0 new messages