The following works in versin 7.9.13, but any suggestions for improvement are greatly appreciated:
Gateway Event Script \ Message named image_upload:
from com.inductiveautomation.ignition.gateway import SRContext
from com.inductiveautomation.ignition.gateway.images import ImageFormat
context = SRContext.get()
image = context.getImageManager()
# 1 Valid image types PNG, JPEG, GIF, BMP
image_type = {
'PNG' : ImageFormat.PNG,
'JPG' : ImageFormat.JPEG,
'JPEG': ImageFormat.JPEG,
'GIF' : ImageFormat.GIF,
'BMP' : ImageFormat.BMP}[payload['image_type']]
image.insertImage(payload['image_name'], payload['image_desc'], image_type, payload['folder'], payload['image_data'], payload['width'], payload['height'], payload['size'])
return
Script on the “Upload” button on the Vision Window:
from java.awt import Toolkit
from java.io import File
from org.apache.commons.io import FilenameUtils
import imghdr
filepath = event.source.parent.getComponent('File Explorer').selectedPath
payload = {}
payload['image_name'] = File(filepath).name
payload['image_desc'] = None
# Valid image types PNG, JPEG, GIF, BMP, fix this in the message handler
payload['image_type'] = imghdr.what(str(filepath)).upper()
# Foward slash is necessary part of folder name
payload['folder'] = "folder/"
javaImage = Toolkit.getDefaultToolkit().getImage(filepath)
payload['width'] = javaImage.width
payload['height'] = javaImage.height
size = File(filepath).length()
payload['size'] = size
payload['image_data'] = system.file.readFileAsBytes(filepath)
# Call the gateway event script message handler, named image_upload on this project
proj = system.tag.read("[System]Client/System/ProjectName").value
status = system.util.sendRequest(proj, 'image_upload', payload)