LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Can I read 16 bit PNG files with LabVIEW?

I have LabVIEW 6.0.2 with the standard IMAQ package (I don't have the Vision Module). I can't figure out how to read 16-bit grayscale PNG files and access all of the data. The standard read function seems to clip the data to 8 bits.
0 Kudos
Message 1 of 9
(5,042 Views)
Hi,
try to use
Functions->Graphics&Sound->Graphics formats->Read PNG file.vi
and set the image depth to 24.

Good luck.

Oleg Chtuko.
0 Kudos
Message 2 of 9
(5,042 Views)
That was the first thing I tried. Unfortunately, it doesn't seem to work. CinePaint can open the images and display them properly, so I don't think there's anything wrong with the files.
0 Kudos
Message 3 of 9
(5,042 Views)
Could you post one of your images?

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 4 of 9
(5,042 Views)
I have attached one of the images. Note that the data uses only 12 bits out of the maximum 16 bits.
0 Kudos
Message 5 of 9
(5,042 Views)
It's a known unsupported image format, here are the unsupported PNG types:

8-bit greyscale - Each pixel is a single byte whose value represents a greyscale level.

16-bit greyscale - Each pixel is a two-byte value which represents a greyscale level.

8-bit,16-bit greyscale w/alpha - Greyscale information is as above but a byte is added for transparency.

We do support 24-bit and 32-bit PNG images.

To be able to load the image into LabVIEW, use an image editor or converter to save the image as a 24-bit or 32-bit PNG, or a BMP or JPG.
0 Kudos
Message 6 of 9
(5,042 Views)
This format appears to be generated by the LabVIEW Vision module, but there's no way to read it back into LabVIEW? I guess I'll have to interface to libpng on my own...
0 Kudos
Message 7 of 9
(5,042 Views)

In the new era of AI and Python, it is easy to do things like this.  I looked up this very question here first, but no help.  I then asked for the python code from chatGPT, and voila, problem solved.  (In my LabVIEW 2019, I also updated the allowed version of python, to 3.10, but it works fine.)  Just passing along a useful tip for those still looking!
python_read_16bit_png.pngread16bitPNGpy.png

0 Kudos
Message 8 of 9
(2,129 Views)

 


@Rod.Loewen wrote:

In the new era of AI and Python, it is easy to do things like this.  I looked up this very question here first, but no help.  I then asked for the python code from chatGPT, and voila, problem solved.  (In my LabVIEW 2019, I also updated the allowed version of python, to 3.10, but it works fine.)  Just passing along a useful tip for those still looking!
read16bitPNGpy.png


For those trying out @Rod.Loewen's code, LV2018 does not support numpy.ndarray as a return type (maybe newer versions do). You'll get following error:

 

Python returned the following error: <class 'SystemError'> g:\a\3\s\objects\listobject.c:189: bad argument to internal function site:forums.ni.com

 

 

I think that's what Rod is trying to achieve in the second part of his python code, but that just copies the first numpy.ndarray to a new numpy.ndarray.

 

Working and verified code:

 

 

import numpy as np
from matplotlib import pyplot as plt

def read_png(path: str):
    # Load the PNG image using matplotlib.
    img = plt.imread(path)
    # Convert the image to a 16-bit integer array using numpy.
    img_16bit = (img * 65535).astype(np.uint16)

    # LabVIEW (2018 at least) doesn't support np.ndarray.
    # Convert to list of lists.
    return img_16bit.tolist()

 

 

read 16bit png_BD.png

 

0 Kudos
Message 9 of 9
(1,555 Views)