# Preserving aspect ratio when running perspective correction

**URL:** <https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433>\
**Category:** 🤝  Community Help\
**Created:** [August 18, 2026, 2:58am UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433 "2026-08-18T02:58:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anthony\_Dunk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/anthony_dunk/32/7186_2.png) [@Anthony\_Dunk](https://discuss.roboflow.com/u/Anthony_Dunk)\
**Post date:** [August 18, 2026, 2:58am UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/1 "2026-08-18T02:58:20Z")

</div>

I have a workflow which detects an outline of a tray in a photo, then does a dynamic zone, then does a perspective correction to warp it to remove the perspective from the photo. This works fine, however the width and height in the perspective correction block are static, but I need them to be based on the dynamic zone output so that the aspect ratio of the tray is preserved. Is there a way to compute the width and height of the output from the dynamic zone block and use this as the width and height in the perspective correction block? Thanks.

---

<div class="post-metadata">

**Author:** ![Automatez](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/automatez/32/8971_2.png) [@Automatez](https://discuss.roboflow.com/u/Automatez)\
**Post date:** [August 19, 2026, 2:16am UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/2 "2026-08-19T02:16:29Z")

</div>

Hey! I think I get what you are going for, but if not feel free to clarify. And in full disclosure, I just used the Roboflow Agent to build out this workflow, but it looks like I would have expected.

Basically added a Custom Python Block to capture the dimensions from the Dynamic Zone Block. Then put those captured variables into the Perspective Correction block.

Whole workflow looks like this:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/7/7a4e7350a51588f4aafb98cf917a7878b4e094a1.png)

Here’s the config for the Python block:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/7/7f2f380f79a68061b1d0c5b850e594373b025e7a.png)

And here’s the Python code:

`def run(self, zones):`  
`if zones is None or len(zones) == 0:`  
`return {"width": 0, "height": 0}`

```auto
valid_polygons = []
for polygon in zones:
    try:
        points = np.asarray(polygon, dtype=np.float64).reshape(-1, 2)
    except (TypeError, ValueError):
        continue
    if len(points) >= 3:
        valid_polygons.append(points)

if len(valid_polygons) == 0:
    return {"width": 0, "height": 0}

polygon = max(valid_polygons, key=lambda p: abs(float(cv2.contourArea(p.astype(np.float32)))))
width = int(round(float(np.max(polygon[:, 0]) - np.min(polygon[:, 0]))))
height = int(round(float(np.max(polygon[:, 1]) - np.min(polygon[:, 1]))))
return {"width": max(0, width), "height": max(0, height)}

```

And here’s the inside of the Perspective Correction Block:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/2/2b9861c3b6fb8873c52803b8ae99ede93ede80a2.png)

Let me know if you have questions!

---

<div class="post-metadata">

**Author:** ![Anthony\_Dunk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/anthony_dunk/32/7186_2.png) [@Anthony\_Dunk](https://discuss.roboflow.com/u/Anthony_Dunk)\
**Post date:** [August 19, 2026, 2:41am UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/3 "2026-08-19T02:41:57Z")

</div>

Thanks for your reply. However, we tried using a custom Python block in Roboflow previously to solve a different issue and they come with a big issue: you cannot run them in a serverless which means running the workflows cost a LOT more. So that pretty much rules out using custom python blocks for us. I was hoping there may be some built in block in Roboflow that does this, but I guess if not, I will just need to perform the perspective correction in my own code outside of Roboflow - effectively ending the workflow at the dynamic zone block. Thanks anyway!

---

<div class="post-metadata">

**Author:** ![Automatez](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/automatez/32/8971_2.png) [@Automatez](https://discuss.roboflow.com/u/Automatez)\
**Post date:** [August 19, 2026, 12:37pm UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/4 "2026-08-19T12:37:41Z")

</div>

I know what you mean. In that case, have you tried Property Definition like below? Maybe that would work for your use-case.

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/c/c1a32b906ebb87f1fdfffdbd0bfdf8d3b1d85227.png)

---

<div class="post-metadata">

**Author:** ![brad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/brad/32/23_2.png) [@brad](https://discuss.roboflow.com/u/brad)\
**Post date:** [August 19, 2026, 4:37pm UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/5 "2026-08-19T16:37:35Z")

</div>

> Thanks for your reply. However, we tried using a custom Python block in Roboflow previously to solve a different issue and they come with a big issue: you cannot run them in a serverless

Good news, [as of last August](https://docs.roboflow.com/reference/changelog/changelog/august-2025/run-custom-python-on-workflows) we support running Custom Python blocks on Serverless!

---

<div class="post-metadata">

**Author:** ![Anthony\_Dunk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/anthony_dunk/32/7186_2.png) [@Anthony\_Dunk](https://discuss.roboflow.com/u/Anthony_Dunk)\
**Post date:** [August 20, 2026, 7:48am UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/6 "2026-08-20T07:48:06Z")

</div>

Thanks. I tried that but kept getting an error about the width and height not being integers which I could not work out how to fix… Do you know how?

 ![image.png](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/c/c15d5fc71d1c77e0b8b15c6697fea365ac2a2517.png)

---

<div class="post-metadata">

**Author:** ![Automatez](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/automatez/32/8971_2.png) [@Automatez](https://discuss.roboflow.com/u/Automatez)\
**Post date:** [August 20, 2026, 1:03pm UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/7 "2026-08-20T13:03:26Z")

</div>

Hopefully I have a fix for you. Here’s what the internal of my Property Definition block looks like. You might just be missing that final operation going to INT via “Convert to Number”.

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/2X/6/6537787d4ec3ce6bc35e808b266d7da46f636d56.png)

---

<div class="post-metadata">

**Author:** ![Anthony\_Dunk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/anthony_dunk/32/7186_2.png) [@Anthony\_Dunk](https://discuss.roboflow.com/u/Anthony_Dunk)\
**Post date:** [August 21, 2026, 3:46am UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/8 "2026-08-21T03:46:56Z")

</div>

Thanks. I appreciate you taking the time to reply again, but I tried this and spent a while trying different things but could not get it to work without errors.

To be honest, I feel like the Perspective Correction block should preserve the aspect ratio of the input by default, without requiring jumping through these hoops. Maybe it will in a future Roboflow update. But for now, I’m doing the perspective correction warp in my own code outside of Roboflow, which is working well for me.

---

<div class="post-metadata">

**Author:** ![Automatez](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/automatez/32/8971_2.png) [@Automatez](https://discuss.roboflow.com/u/Automatez)\
**Post date:** [August 21, 2026, 5:17pm UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/9 "2026-08-21T17:17:56Z")

</div>

YW. Wish we could have found the issue. Good point - it’s a Feature Request they might add to the list!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/bb6edbfa56d0cf2ee603f061e9e09103502cc8cc.png) [@system](https://discuss.roboflow.com/u/system)\
**Post date:** [September 11, 2026, 5:18pm UTC](https://discuss.roboflow.com/t/preserving-aspect-ratio-when-running-perspective-correction/12433/10 "2026-09-11T17:18:41Z")

</div>

Closed on September 11, 2026, 5:18pm UTC

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
