Autoscale Image to Fill Bounds but Maintain Aspect Ratio

I finally got back to this topic and found a solution. The key was finding the ImageLoader function, which lets me find the original aspect ratio of the image regardless of however its currently scaled.

from com.inductiveautomation.ignition.client.images import ImageLoader

#Find Aspect Ratio of Component
imgComponent = system.gui.getWindow("Main Windows/General/Login").rootContainer.getComponent("Image")
imgComponentW = imgComponent.getWidth()
imgComponentH = imgComponent.getHeight()
imgComponentRatio = float(imgComponentW) / float(imgComponentH)

#Find Aspect Ratio of Original Image
filename = imgComponent.getPath()
img = ImageLoader.getInstance().loadImage(filename)
imgW = img.getWidth()
imgH = img.getHeight()
imgRatio = float(imgW) / float(imgH)

##Find Component Image Size in "% Bounds"
#if (imgComponentRatio >= imgRatio):
#	fillW = 100
#	fillH = int(fillW / imgRatio * imgComponentRatio)
#else:
#	fillH = 100
#	fillW = int(fillH * imgRatio / imgComponentRatio)
#
## Set Strech Mode to "% Bounds"
#imgComponent.setStretchMode(3)

#Find Component Image Size in "Pixel Size"
if (imgComponentRatio >= imgRatio):
	fillW = imgComponentW
	fillH = int(fillW / imgRatio )
else:
	fillH = imgComponentH
	fillW = int(fillH * imgRatio )

# Set Strech Mode to "Parameters"
imgComponent.setStretchMode(2)

#Set Component Image Bounds
imgComponent.setStretchHeight(fillH)
imgComponent.setStretchWidth(fillW)
3 Likes