This is how to convert alpha values into mask values.
import array
import wx
app = wx.App( redirect=False ) # No Frame or MainLoop, but can call all methods.
filename = ‘Toucan.PNG’
wxImg = wx.Image( filename, wx.BITMAP_TYPE_ANY )
hasMask = wxImg.HasMask()
hasAlpha = wxImg.HasAlpha()
print '---- hasMask, hasAlpha = ', hasMask, hasAlpha
Threshold the transparency layer values to [0, non-zero].
if hasAlpha : # Is always mutually exclusive with hasAlpha.
# Get the alpha values
alphaTransparencyString = wxImg.GetAlphaBuffer()
alphaTransparencyStringLen = len( alphaTransparencyString )
maskThreshhold = 128 # Use any suitable value [0..255].
maskTransparencyArray = array.array( 'B' ) # chr = unsigned int8
maskTransparencyArray.fromstring( alphaTransparencyString )
for index in xrange( alphaTransparencyStringLen ) :
aTranspValue = maskTransparencyArray[ index ]
if index > int( 0.99 * alphaTransparencyStringLen) :
print 'alpha =', aTranspValue ,
if (aTranspValue < maskThreshhold) :
maskTransparencyArray[ index ] = 0
else :
maskTransparencyArray[ index ] = 255 # Can set to any value [1..255]
#end if
if index > int( 0.99 * alphaTransparencyStringLen) :
print '-->', maskTransparencyArray[ index ]
#end for
maskTransparencyString = maskTransparencyArray.tostring()
#end if
newWxImg = wxImg
newWxImg.SetAlphaData( maskTransparencyString )
newWxImg.SaveFile( ‘Toucan_New_Image.PNG’, wx.BITMAP_TYPE_PNG )
newWximport array
import wx
app = wx.App( redirect=False ) # No Frame or MianLoop, but can call all methods.
filename = ‘Toucan.PNG’
wxImg = wx.Image( filename, wx.BITMAP_TYPE_ANY )
hasMask = wxImg.HasMask()
hasAlpha = wxImg.HasAlpha()
print '---- hasMask, hasAlpha = ', hasMask, hasAlpha
Threshold the transparency layer values to [0, non-zero].
if hasAlpha : # Is always mutually exclusive with hasAlpha.
# Get the alpha values
alphaTransparencyString = wxImg.GetAlphaBuffer()
alphaTransparencyStringLen = len( alphaTransparencyString )
maskThreshhold = 128 # Use any suitable value [0..255].
maskTransparencyArray = array.array( 'B' ) # chr = unsigned int8
maskTransparencyArray.fromstring( alphaTransparencyString )
for index in xrange( alphaTransparencyStringLen ) :
aTranspValue = maskTransparencyArray[ index ]
if index > int( 0.99 * alphaTransparencyStringLen) :
print 'alpha =', aTranspValue ,
if (aTranspValue < maskThreshhold) :
maskTransparencyArray[ index ] = 0
else :
maskTransparencyArray[ index ] = 255 # Can set to any value [1..255]
#end if
if index > int( 0.99 * alphaTransparencyStringLen) :
print '-->', maskTransparencyArray[ index ]
#end for
maskTransparencyString = maskTransparencyArray.tostring()
#end if
newWxImg = wxImg
newWxImg.SetAlphaData( maskTransparencyString )
newWxImg.SaveFile( ‘Toucan_New_Image.PNG’, wx.BITMAP_TYPE_PNG )
newWxBmap = newWxImg.ConvertToBitmap()
newWxBmap.SaveFile( ‘Toucan_New_Bitmap.PNG’, wx.BITMAP_TYPE_PNG )
Bmap = newWxImg.ConvertToBitmap()
newWxBmap.SaveFile( ‘Toucan_New_Bitmap.PNG’, wx.BITMAP_TYPE_PNG )
···
#-----------------------
Ray