(asset_fname, background_rgba)
return png_encoding
Decode PNG from asset file and add solid background.
Decode PNG from asset file and add solid background.
Decode PNG from asset file and add solid background .
def _asset_png_with_background_rgba_bytes(asset_fname, background_rgba):
  """Decode PNG from asset file and add solid background."""

  # Retrieve PNG image contents as a bytestring, convert to a numpy array.
  contents = resources.GetResource(os.path.join(_ASSETS_PATH, asset_fname))
  digit_rgba = np.array(Image.open(io.BytesIO(contents)), dtype=np.double)

  # Add solid background with `background_rgba`.
  blended = 255. * _alpha_blend(digit_rgba / 255., np.asarray(background_rgba))

  # Encode composite image array to a PNG bytestring.
  img = Image.fromarray(blended.astype(np.uint8), mode='RGBA')
  buf = io.BytesIO()
  img.save(buf, format='PNG')
  png_encoding = buf.getvalue()
  buf.close()

  return png_encoding
def _asset_png_with_background_rgba_bytes ( asset_fname , background_rgba ) : # Retrieve PNG image contents as a bytestring, convert to a numpy array. contents = resources . GetResource ( os . path . join ( _ASSETS_PATH , asset_fname ) ) digit_rgba = np . array ( Image . open ( io . BytesIO ( contents ) ) , dtype = np . double ) # Add solid background with `background_rgba`. blended = 255. * _alpha_blend ( digit_rgba / 255. , np . asarray ( background_rgba ) ) # Encode composite image array to a PNG bytestring. img = Image . fromarray ( blended . astype ( np . uint8 ) , mode = 'RGBA' ) buf = io . BytesIO ( ) img . save ( buf , format = 'PNG' ) png_encoding = buf . getvalue ( ) buf . close ( ) return png_encoding