# How to zoom into an image or video frame with OpenCV using array slicing

**URL:** <https://www.robotexchange.io/t/how-to-zoom-into-an-image-or-video-frame-with-opencv-using-array-slicing/4742>\
**Category:** 💡 How-to\
**Tags:** Python, open-cv\
**Created:** [September 2, 2026, 12:18am UTC](https://www.robotexchange.io/t/how-to-zoom-into-an-image-or-video-frame-with-opencv-using-array-slicing/4742 "2026-09-02T00:18:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![thijs](https://www.robotexchange.io/user_avatar/www.robotexchange.io/thijs/32/4_2.png) [@thijs](https://www.robotexchange.io/u/thijs)\
**Post date:** [September 2, 2026, 12:18am UTC](https://www.robotexchange.io/t/how-to-zoom-into-an-image-or-video-frame-with-opencv-using-array-slicing/4742/1 "2026-09-02T00:18:43Z")

</div>

_Based on contributions by Jasmijn._

“Zooming in” on part of an image in OpenCV doesn’t need any special function, it’s just cropping a rectangle out of the image array. This how-to explains why that works and gives copy-pasteable code, for anyone who doesn’t yet know how OpenCV stores images or how array slicing works.

## What you need

- A computer with Windows, macOS or Linux.
- Python 3.8 or higher.
- OpenCV: `pip install opencv-python`
- An image to test with (a photo of your own, or any PNG/JPG).

## Steps

### 1. Understand that images in OpenCV are arrays

When you load an image with OpenCV, it’s stored as a matrix of pixel values, one 3-value entry (Blue, Green, Red) per pixel. So a color image is a 3-dimensional array:

```auto
image[row, column, color_channel]

```

- `row` moves top to bottom
- `column` moves left to right
- `color_channel` is B, G, R (in that order — OpenCV uses BGR, not RGB)

You can build a tiny 3x3 image by hand to see this in practice:

 ![create_3x3_ColorImage](https://www.robotexchange.io/uploads/default/original/2X/6/67053e0f2219424db32a6532957cfe9b812326b4.jpeg)

Which, enlarged so the individual pixels are visible, looks like:

 ![small_image_enlarged](https://www.robotexchange.io/uploads/default/original/2X/2/293be7dcf9319de803c53548a107ef118d1188ca.png)

### 2. Understand slicing

Slicing takes a section out of an array. For a simple 1D array:

 ![geeksforgeeks 1-D Array Slicing](https://www.robotexchange.io/uploads/default/original/2X/9/97b88cbb12888b288819e2431c5ce135037d0384.png)

The same idea extends to multi-dimensional arrays, such as an image:

 ![geeksforgeeks Python Slicing Subarrays](https://www.robotexchange.io/uploads/default/original/2X/1/1bb0f08d65ffd1cfde2cb61a466014476fcadba3.png)

In that example, the matrix is sliced down to its first two rows and its last two columns.

### 3. Slice the image to zoom in

Since an image is just an array, zooming in is selecting a smaller rectangle from it with slice indexing:

 ![syntax_image_zoom](https://www.robotexchange.io/uploads/default/original/2X/1/11f820ada419a441fe4d151037233fc2f9be635d.png)

```auto
new_image = original_image[top:bottom, left:right]

```

The pixel coordinate system in OpenCV has `(0, 0)` at the top-left corner, with coordinates increasing downward and to the right:

```auto
   top, left top, right
      (0,0) ------------------- (50, 0)
        | |
        | |
        | |
     (0, 50) ------------------ (50, 50)
   bottom, left bottom, right

```

Keep in mind that pixel coordinates are integers (whole numbers, no decimals).

### 4. Example: zoom into two regions of an image

This crops two rectangles out of a banner image and shows all three (original + two zoomed regions). Press any key to save the crops to disk and exit.

```python
import cv2

try:
    # Open the image
    image = cv2.imread("banner-4000x2000.png")
    ratio = 4000 / 2000

    # Resize the image so it doesn't go bigger than your screen
    # (keep the ratio the same as the original image)
    width = 1200
    height = int(width / ratio)
    image = cv2.resize(image, (width, height))

    # Zoom into the window with the robot arm in it
    top_robot_window = 190
    bottom_robot_window = 450
    left_robot_window = 850
    right_robot_window = 1095

    zoomed_robot_window = image[
        top_robot_window:bottom_robot_window,
        left_robot_window:right_robot_window
    ]

    # Zoom into the text "SMR DELFT"
    top_smr_delft = 190
    bottom_smr_delft = 360
    left_smr_delft = 550
    right_smr_delft = 800

    zoomed_smr_delft = image[
        top_smr_delft:bottom_smr_delft,
        left_smr_delft:right_smr_delft
    ]

    # Show the images
    cv2.imshow("original_resized", image)
    cv2.imshow("zoomed_robot_window", zoomed_robot_window)
    cv2.imshow("zoomed_smr_delft", zoomed_smr_delft)

    # Wait for a key press, then save and exit
    cv2.waitKey(0)

    cv2.imwrite("original_resized.png", image)
    cv2.imwrite("zoomed_robot_window.png", zoomed_robot_window)
    cv2.imwrite("zoomed_smr_delft.png", zoomed_smr_delft)

finally: # Makes sure all windows get closed, even if an error occurs
    cv2.destroyAllWindows()

```

> `cv2.waitKey(time)` waits `time` milliseconds for a key press before continuing (`0` waits forever). For a still image, `0` or `1` is fine. For video or a live feed, a low value keeps up with the camera and a high value slows playback down. Wrap webcam code in `try`/`finally` like above so the camera is always released, even if the script is interrupted or crashes.

## Common mistakes

- Mixing up rows and columns: the first slice index is always the **row** (vertical, `top:bottom`), the second is the **column** (horizontal, `left:right`). It’s easy to swap them by accident.
- Using float coordinates. Slice bounds must be integers.
- Forgetting the image is BGR, not RGB, when working with colors directly.

## Related

- [How to detect multiple data matrix codes fast using threading in Python](https://www.robotexchange.io/t/how-to-detect-multiple-data-matrix-codes-fast-using-threading-in-python/4743)

* * *

_Rewritten and consolidated (Sept 2026) from the original student how-to’s: [How to zoom in on an image or frame using opencv](https://www.robotexchange.io/t/how-to-zoom-in-on-an-image-or-frame-using-opencv/4109)._
