Python Script to convert varbinary in SQL Server to base64 for Perspective Image Component

I have file data stored in my database as varbinary. Sometimes the files are images. I want my image component to display the images, but I think I’m having an issue converting from the varbinary data to base64. I can convert the varbinary data to base 64, but the image component does not display.

Here is the image I uploaded using the perspective upload component, and then pushed to the db as varbinary:

nav_icon

Here’s the script I used to transform the varbinary data to base64 data:

	
	import base64
	import json
	
	data = list(my_varbinary_data])	
	dataStr = json.dumps(data)
	base64EncodedStr = base64.b64encode(dataStr.encode('utf-8'))
	return  "data:imagef;base64,%s"%base64EncodedStr





The resulting base64 encoded data doesn’t get displayed on the image component:

varbinary_data.txt (43.0 KB)

varbinary_data_base_64_encoded.txt (34.8 KB)

Another Thing I’ve noticed is that for some reason my ignition transforms the file_bytes from the native ASCII encoded data into an integer array when I try and pull the data out of the database?

image

Bytes are signed in java. Those are your unmodified bytes.

1 Like

I tried a script that convereted each byte of the array to an unsigned integer by %256 for each element. That didnt seem to actually make the image component render the base64 encoded data though. I can download the byte data from SQL and open the file and it is correct.

Is the main crux of the problem that I first need to convert each element in the list to unisgned data? I tried that before and it didn’t seem to completely solve the problem.

I don’t understand why there’s a json conversion in your code. Use b64encode on the raw data from the DB, then present that in your data URL.

1 Like