Could wxBitmap be changed so that if a type for the bitmap isn't passed to
the constructor, LoadFile, or SaveFile, the type would be determined by the
filename extension automatically? I'm already using a variation of the
following function in PythonCard for the Bitmap class.
def bitmapType(filename):
"""
Get the type of an image from the file's extension ( .jpg, etc. )
"""
if filename == '':
return None
name, ext = os.path.splitext(filename)
ext = ext[1:].upper()
if ext == 'BMP':
return wx.wxBITMAP_TYPE_BMP
elif ext == 'GIF':
return wx.wxBITMAP_TYPE_GIF
elif ext == 'JPG' or ext == 'JPEG':
return wx.wxBITMAP_TYPE_JPEG
elif ext == 'PCX':
return wx.wxBITMAP_TYPE_PCX #elif ext == 'PICT':
# return wx.wxBITMAP_TYPE_PICT
elif ext == 'PNG':
return wx.wxBITMAP_TYPE_PNG
elif ext == 'PNM':
return wx.wxBITMAP_TYPE_PNM
elif ext == 'TIF' or ext == 'TIFF':
return wx.wxBITMAP_TYPE_TIF
elif ext == 'XBM':
return wx.wxBITMAP_TYPE_XBM
elif ext == 'XPM':
return wx.wxBITMAP_TYPE_XPM
else:
# rather than throw an exception, we could try
# and have wxPython figure out the image
# type by returning wxBITMAP_TYPE_ANY
# otherwise should throw an exception here
raise 'invalid graphics format'
I've been converting PythonCard components and other framework elements to
use direct subclasses of wxPython, and this is one of the classes which is
probably going to go away, but I like the convenience of the type
automatically being figured out.
> Could wxBitmap be changed so that if a type for the bitmap
isn't passed to
> the constructor, LoadFile, or SaveFile, the type would be determined by
the
> filename extension automatically?
Try wxBITMAP_TYPE_ANY. I know it works with wxImage and it probably will
with wxBitmap too.
Okay, maybe it didn't work in 2.3.1 when I wrote that routine, but it seems
to be working now. So, if wxBITMAP_TYPE_ANY works, then why not have that be
the default for the type parameter? That way we can just do:
bmp = wx.wxBitmap('edit.gif')
and
bmp.LoadFile('trash.gif')
instead of
bmp = wx.wxBitmap('edit.gif', wx.wxBITMAP_TYPE_ANY)
or
bmp.LoadFile('trash.gif', wx.wxBITMAP_TYPE_ANY)
SaveFile can't currently use wx.wxBITMAP_TYPE_ANY, so it needs to figure out
the type based on the extension.
Try wxBITMAP_TYPE_ANY. I know it works with wxImage and it
probably will with wxBitmap too.
It does (in C++; and wxBITMAP_TYPE_ANY is the default), but not for
saving. Guessing save format from extension is not a good idea
(within the library, it's probably ok in your app!), because it would
not work always (think names like EWRKdsdDF.001 or saving into a
stream directly, w/o the knowledge of filename). It would be too
confusing to have two different semantics for wxBITMAP_TYPE_ANY
("detect file format from file's content" for loading and "guess file
format based on the extension, if present and known" for saving).