WebDev and Images

i am implementing a webdev script to call images from the database and host them to perspective and to vision applications. All is working fine for image components that bind the URL.

However, now I need to get the byte array to put in a paintable canvas cached image and I need to access the image bytes through scripting.

The script below returns what appears to be a string and the system.net.httpGet documentation shows the return value as a string. How can I convert this to the correct byte array?

ID = 18
Server = system.tag.read("[System]Client/Network/GatewayAddress").value
Project = system.tag.read("[System]Client/System/ProjectName").value
url = Server + "/system/webdev/" + Project +  "/EquipmentFiles?ID=" + str(ID)
print url
data = system.net.httpGet(url)
print data

Consider using system.util.sendRequest() to return the image bytes. If you have factored the meat of your image retrieval script into a project script module, it should be much simpler than decoding the server’s http(s) response.

The challenge is that the web dev module is already working very well for image components on both Perspective and Vision projects.

I’d prefer to not need to build a whole other method to return the byte array, when the web dev script is working

for reference here is the web dev script for the doGet

	params = request['params']
	ID = params['ID']
	data = system.db.runScalarPrepQuery("select fileData from spares_data where spares_data_id = ? ",[ID])
	
	
	return {'bytes': data}

Can you use

ImageIO.read(url)

Hey everyone, thanks for the help!

Here is what I ended up with. To put the image into the “img-cached” property then I needed to write the result of ImageIO.read to a byteArrayOutputStream and then call .toByteArray()

from java.io import ByteArrayInputStream
from java.io import ByteArrayOutputStream
from javax.imageio import ImageIO
from java.awt import Image
from java.net import URL

Server = system.tag.read("[System]Client/Network/GatewayAddress").value
Project = system.tag.read("[System]Client/System/ProjectName").value
url = URL(Server + "/system/webdev/" + Project +  "/EquipmentFiles?ID=" + str(ID))
print url
d = ImageIO.read(url)


bos = ByteArrayOutputStream()
ImageIO.write(d, "jpg", bos )
data = bos.toByteArray()