Display image stored as a byte array in database

I get images as bytes by a file upload component and stored them into the database, now i want to fetch byte array and convert them into images. Is there any way or script to do that?
Thank you in advance.

From what I've read on the forum you should be storing the images in a BLOB field.

2 Likes

Thank you for your response,
There is no other way to convert byte array into an image in ignition?

Of course. You can import the appropriate classes from Java to construct the Image from a Byte array.

Something like:

from javax.imageio import ImageIO
from java.io import ByteArrayInputStream

def bufferedImageFromBytes(bytes):
    return ImageIO.read(ByteArrayInputStream(bytes))

This will return a BufferedImage object, you can then use the methods there to do what you need with the Image. That's a whole lot of work though, personally I would stick with the BLOBs.

2 Likes

Arguably, the BLOB is the image. It certainly is the form most useful for display in Perspective.

If you want to process the image in some way before display (in Perspective), you will need to follow @lrose's advice in a WebDev script, writing the modified image back to a byte array before delivering to the browser. Or, perhaps, process the image right after upload and store the altered image to the DB as a blob.

One way or the other, BLOB (byte array) is the right format for sending the image around your network.

3 Likes

Thank you lrose, I'll try this.

Thank you for elaborating pturmel, that's really useful.

Hi Jay,

You can convert your byte stream to Base64 and display it as follows;

	from java.util import Base64
	b64Image = Base64.getEncoder().encodeToString(bytesStreamImage)
	self.getSibling("Image").props.source = "data:image/png;base64," + b64Image

Using data urls with base64 is terribly inefficient--bogs down both Perspective on the gateway and the client browser.

Deliver images as image bytes via URL.