08-05-2024 10:33 AM
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
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
Solved! Go to Solution.
08-05-2024 11:26 AM
@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
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.
08-05-2024 12:57 PM
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.
08-07-2024 11:24 AM
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:
08-07-2024 12:09 PM
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