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

**URL:** <https://forum.inductiveautomation.com/t/python-script-to-convert-varbinary-in-sql-server-to-base64-for-perspective-image-component/57478>\
**Category:** Ignition\
**Tags:** scripting, perspective\
**Created:** [March 22, 2022, 6:47pm UTC](https://forum.inductiveautomation.com/t/python-script-to-convert-varbinary-in-sql-server-to-base64-for-perspective-image-component/57478 "2022-03-22T18:47:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmahaf](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tmahaf/32/23492_2.png) [@tmahaf](https://forum.inductiveautomation.com/u/tmahaf)\
**Post date:** [March 22, 2022, 6:47pm UTC](https://forum.inductiveautomation.com/t/python-script-to-convert-varbinary-in-sql-server-to-base64-for-perspective-image-component/57478/1 "2022-03-22T18:47:21Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/3/a3d77edd894eae7e73ac6bf81cf5c4ea7102b3b9.png)

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

```auto
	
	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:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/7/27322eb3af4990c74d0c4624f7534ec953394d01.png)

[varbinary\_data.txt](https://forum.inductiveautomation.com/uploads/short-url/lLbVtWENQO1ugTUd7WXelINCTaZ.txt) (43.0 KB)

[varbinary\_data\_base\_64\_encoded.txt](https://forum.inductiveautomation.com/uploads/short-url/w5OVfdRY2saHlFdkOHyPHQeT9bq.txt) (34.8 KB)

---

<div class="post-metadata">

**Author:** ![tmahaf](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tmahaf/32/23492_2.png) [@tmahaf](https://forum.inductiveautomation.com/u/tmahaf)\
**Post date:** [March 22, 2022, 8:06pm UTC](https://forum.inductiveautomation.com/t/python-script-to-convert-varbinary-in-sql-server-to-base64-for-perspective-image-component/57478/2 "2022-03-22T20:06:57Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/3/c3792de90836f59cacb0efc4ded2c30267a68cf8.png)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/d/edd8c19208f6c09e1cc0b2bd775f59a521b4e79b.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 22, 2022, 9:01pm UTC](https://forum.inductiveautomation.com/t/python-script-to-convert-varbinary-in-sql-server-to-base64-for-perspective-image-component/57478/3 "2022-03-22T21:01:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tmahaf](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tmahaf/32/23492_2.png) [@tmahaf](https://forum.inductiveautomation.com/u/tmahaf)\
**Post date:** [March 22, 2022, 11:13pm UTC](https://forum.inductiveautomation.com/t/python-script-to-convert-varbinary-in-sql-server-to-base64-for-perspective-image-component/57478/4 "2022-03-22T23:13:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 22, 2022, 11:19pm UTC](https://forum.inductiveautomation.com/t/python-script-to-convert-varbinary-in-sql-server-to-base64-for-perspective-image-component/57478/5 "2022-03-22T23:19:21Z")

</div>

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.
