# How to get Width and Height of image in perspective

**URL:** <https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853>\
**Category:** Ignition\
**Created:** [August 18, 2020, 1:21pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853 "2020-08-18T13:21:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anita.Shinde](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Anita.Shinde](https://forum.inductiveautomation.com/u/Anita.Shinde)\
**Post date:** [August 18, 2020, 1:21pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/1 "2020-08-18T13:21:05Z")

</div>

Hi,  
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?  
Thanks.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 18, 2020, 4:24pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/2 "2020-08-18T16:24:27Z")

</div>

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:

> [@Image Management Tool](http://forum.inductiveautomation.com/t/image-management-tool/37558/2):
>
> from java.awt import Toolkit from java.io import File from org.apache.commons.io import FilenameUtils from com.inductiveautomation.ignition.designer.gateway import DTGatewayInterface gi = DTGatewayInterface.instance filepath = system.file.openFile() if filepath is not None: name = File(filepath).name javaImage = Toolkit.defaultToolkit.getImage(filepath) gi.uploadImage(event.source, name, "Script uploaded image", FilenameUtils.getExtension(filepath), "", javaImage.width, javaImage.height, sy…

Specifically, the `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.

---

<div class="post-metadata">

**Author:** ![Anita.Shinde](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Anita.Shinde](https://forum.inductiveautomation.com/u/Anita.Shinde)\
**Post date:** [August 18, 2020, 5:12pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/3 "2020-08-18T17:12:55Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 18, 2020, 5:17pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/4 "2020-08-18T17:17:53Z")

</div>

You don’t need everything else in the example. Only the Toolkit import and related line.

---

<div class="post-metadata">

**Author:** ![Anita.Shinde](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Anita.Shinde](https://forum.inductiveautomation.com/u/Anita.Shinde)\
**Post date:** [August 18, 2020, 5:24pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/5 "2020-08-18T17:24:57Z")

</div>

So, In that case ,'object has no attribute ‘openFile’ this error occures.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/d/4d8d1847694ee7ad04198764998e4318f2370506.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 18, 2020, 5:28pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/6 "2020-08-18T17:28:55Z")

</div>

system.file.openFile() is a gui operation for selecting a file path. For Vision only.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 18, 2020, 5:31pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/7 "2020-08-18T17:31:31Z")

</div>

In the file upload component, save your uploaded file to a temporary directory:  
[https://docs.inductiveautomation.com/display/DOC80/Perspective+-+File+Upload](https://docs.inductiveautomation.com/display/DOC80/Perspective+-+File+Upload)  
[https://docs.inductiveautomation.com/display/DOC80/system.file.getTempFile](https://docs.inductiveautomation.com/display/DOC80/system.file.getTempFile)

Then parse it with Toolkit.

---

<div class="post-metadata">

**Author:** ![Anita.Shinde](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Anita.Shinde](https://forum.inductiveautomation.com/u/Anita.Shinde)\
**Post date:** [August 18, 2020, 6:24pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/8 "2020-08-18T18:24:17Z")

</div>

I am trying this way but getting this error.  
type object ‘java.awt.Toolkit’ has no attribute 'defaultToolkit  
How can I correct this?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/9/7921ea4c64684e037ab0f6fd8b7a0874fb650fe8.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 18, 2020, 6:42pm UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/9 "2020-08-18T18:42:37Z")

</div>

I believe it should be:

```auto
javaImage = Toolkit.getDefaultToolkit().getImage(imagePath)

```

Instead of just defaultToolkit

---

<div class="post-metadata">

**Author:** ![Anita.Shinde](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Anita.Shinde](https://forum.inductiveautomation.com/u/Anita.Shinde)\
**Post date:** [September 15, 2020, 9:51am UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/10 "2020-09-15T09:51:40Z")

</div>

> [@lrose](#):
>
> getDefaultToolkit

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?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 15, 2020, 10:26am UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/11 "2020-09-15T10:26:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Anita.Shinde](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Anita.Shinde](https://forum.inductiveautomation.com/u/Anita.Shinde)\
**Post date:** [September 15, 2020, 10:54am UTC](https://forum.inductiveautomation.com/t/how-to-get-width-and-height-of-image-in-perspective/37853/12 "2020-09-15T10:54:00Z")

</div>

> [@lrose](#):
>
> Image.getScaledInstance()

How can use Image.getScaledInstance()?
