LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Saving IMAQ image as PNG in MongoDB and exporting it with Python

Solved!
Go to solution

Hi,

I am aquiring image data with the LabView IMAQ module.
I convert the acquired image to a png string with IMAQ Write String VI and store that string in a MongoDB database.
If I fetch this string back from the database, write it to a file with Write to Binary File VI and read that file with IMAQ Read from File, I can display that image.

I can also open the created png file in Windows without problems.

Mock up VI

kbmp_0-1722871623783.png

 

If I fetch the png string from the database in Python and do basically the same procedure to write the string to a binary file with:

 

raw_string_from_db = 'pngstringdata'
encoded_data = raw.encode('utf-8')
with open('test.png', 'wb') as png_file:
    png_file.write(encoded_data)

 

I can't open the image in windows or display it in any other way. I have tried converting it to base64, latin1, cp1252, and probably 10 more different encodings, nothing helps.

Does anybody have an idea what might be the problem here?

- kbmp

 

0 Kudos
Message 1 of 5
(288 Views)

@kbmp- wrote:

Hi,

I am aquiring image data with the LabView IMAQ module.
I convert the acquired image to a png string with IMAQ Write String VI and store that string in a MongoDB database.
If I fetch this string back from the database, write it to a file with Write to Binary File VI and read that file with IMAQ Read from File, I can display that image.

I can also open the created png file in Windows without problems.

Mock up VI

kbmp_0-1722871623783.png

 

If I fetch the png string from the database in Python and do basically the same procedure to write the string to a binary file with:

 

raw_string_from_db = 'pngstringdata'
encoded_data = raw.encode('utf-8')
with open('test.png', 'wb') as png_file:
    png_file.write(encoded_data)

 

I can't open the image in windows or display it in any other way. I have tried converting it to base64, latin1, cp1252, and probably 10 more different encodings, nothing helps.

Does anybody have an idea what might be the problem here?

- kbmp

 


The problem is you're encoding it with "utf-8" which is for text data but your data is binary.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 2 of 5
(266 Views)

How would I have to change the Python code?

When my REST API receives the png string from LabView in the json payload, it encodes it to utf-8 and stores it in gridfs.
When retrieving the string back from the database, I reencode it into utf-8 and send it back via the REST API as a json payload.

LabView seems to have no problem using that string and storing it into a binary file with a .png file ending but my Python code won't work.

 

 

0 Kudos
Message 3 of 5
(255 Views)
Solution
Accepted by topic author kbmp-

LabView seems to have no problem using that string and storing it into a binary file with a .png file ending but my Python code won't work.


LabVIEW strings are pretty much just byte arrays so that's why the string data can be written directly to a binary file. The issue is that arbitrary byte arrays are not proper UTF-8 encoded strings. Certain LabVIEW string byte sequences are not valid UTF-8 byte sequences.

 

Some ideas:

 

  • If your database and python can handle byte arrays and binary data directly, then use String to Byte Array and handle it as a byte array.
  • If you need to pass things around as a string then first base64 encode the binary data to a base64 encoded string, pass the base64 encoded string to the database, and base64 decode the string back to binary data in python.

 


Milan
0 Kudos
Message 4 of 5
(209 Views)
Solution
Accepted by topic author kbmp-

That is what I figured after some more research and troubleshooting.

When I send the PNG String from LabView to the REST API written in Python, I first convert the LabView cluster to a json string, then send it to the API with a HTTP GET request.

For some reason the API doesn't have a problem handeling the png string contained in the json payload, store and retrieve it from the database, send it back to LabView and everything just works.

 

But when I retrieve the json document with a GET request performed in a Python script with the requests package, the png string gets malformed somehow, presumably because Python doesn't know that it should be a bytestring and therefore I can't write it into a binary file, without encoding it somehow.

Since I dont have that many documents in my database yet, my approach is to retrieve all png strings from the database with LabView, encode the byte string with base64 and replace the string currently present in the database with that string.

This way the retrieval from LabView and Python works fine and I can safely restore my image data across different programming languages.

-kmbp

Message 5 of 5
(204 Views)