I am using file upload component to upload the image in perspective, But I want to upload image in 16:9 format.
To calculate aspect ratio I need width and height of uploaded image.
How can I achieve this in perspective?
You can’t determine the aspect ratio until the image is uploaded. If it’s in a common format (PNG, GIF, JPG) then you can borrow from the technique from this post:
Toolkit.getDefaultToolkit().getImage() - once loaded through that mechanism, you can get the width and height of an image, and from there determine the aspect ratio.
I have tried this but it is throwing an error. i.e. “No module named gateway”.
for this line ‘from com.inductiveautomation.ignition.designer.gateway import DTGatewayInterface’
You don’t need everything else in the example. Only the Toolkit import and related line.
system.file.openFile() is a gui operation for selecting a file path. For Vision only.
In the file upload component, save your uploaded file to a temporary directory:
Then parse it with Toolkit.
I am trying this way but getting this error.
type object ‘java.awt.Toolkit’ has no attribute 'defaultToolkit
How can I correct this?
I believe it should be:
javaImage = Toolkit.getDefaultToolkit().getImage(imagePath)
Instead of just defaultToolkit
By using this I am getting image Height and Width but if I resize any image from paint or any other tool and try to upload then this is giving -1 height and width.
Why it is giving -1 , any idea?
Java Documentation says that Image.getWidth() returns -1 when the width is not yet known. Similar behavior is documented for Image.getHeight().
This would suggest that you are trying to retrieve the height and width before the image has been completely loaded.
I would suggest that you use Image.getScaledInstance() to generate a new scaled image with your desired aspect ratio.
How can use Image.getScaledInstance()?