Removing the Transparency Layer From a PNG Image in NI LabVIEW

Updated Jun 8, 2023

Reported In

Software

  • Vision Development Module
  • LabVIEW

Driver

  • NI-IMAQdx

Issue Details

  • How can I import a 32-bit PNG image and replace it's transparent background with a custom color? 
  • I know that alpha channel is not supported in NI Vision Development Module but how can I import just the alpha channel values from an image?
  • I'm facing Error '-1074395247: Invalid Template Descriptor' when I try to create a pattern matching template out of a white colored shape/logo. 

Solution

You can achieve PNG image Alpha channel manipulation using the PNG Read/Write VIs available in Graphics Formats Library, LabVIEW  Base Development System.
Refer to the snippet below for an example of how to use the PNG library to manipulate the Transparency (Alpha) channel.
  • Use the Read PNG.vi to get the 32-bit PNG image data.
  • Use Unflatten Pixmap VI to convert the image data into a Pixmap. Note that this VI does not support 32-bit pixmaps and hence, will convert the 32-bit image data into a 24-bit pixmap.
  • Image data contains, among other things, the image bit-stream. Since this is a 32-bit image, that means there are 4 values dedicated to each pixel: Alpha, Red, Green and Blue. 
  • Replace Zero Alpha (Transparent) Values with the Transparent Color Box.
  • Use IMAQ Create.vi to create a new image with custom background and overlay the Bitmap/Pixmap you created in the last step.
  • Log this image and you have successfully converted a 32-bit PNG image to a 24-bit PNG image with the transparency converted to a color.


Results:
  
                                                     Test 1                                             Result 1


  
                                                Test 2  (White Logo)                               Result 2

Additional Information

PNG Images have a transparency layer called the Alpha layer. This is not supported in NI Vision. The transparent pixels are converted to white pixels when you import a 32-bit PNG image using NI Vision. A white logo/shape on a transparent background will be imported as a plain white image in LabVIEW, hence losing its edges and subsequently throwing out the 'Invalid Descriptor' error.

PNG8 Images (Indexed) are not supported in NI Vision as well as PNG Read/Write VIs (available in Graphics Formats Library). The 8-bit index will be interpreted as an 8-bit true pixel value. The PNG Read/Write VIs don't import indexing information.