# How to Resize an Image with OpenCV

So you want to resize an image with OpenCV? You've found your answer. This tutorial will walk you through all the steps needed to do this.

I will use Python in this example, but it's a similar process in many different languages. I often work with OpenCV in Go and C# as well. But let's stay on topic here.

This will be a step-by-step guide to quickly resize and save an image using OpenCV.

## Step 1: Create a Python Virtual Environment

Whenever I do anything with Python, I create a Virtual Environment. It's like a container for your Python application. It makes it portable and easy to manage dependencies. I'm using Linux for this, but it's the same commands in other operating systems.

Open up a terminal window and choose the directory you'll be building your application under. I'm using a directory named "imageresizer" because I'm great at naming things creatively.

Type in:

```plaintext
python -m venv imageresizer
```

I'm going to name the Virtual Environment as "imageresizer". Again creativity. Let's activate the environment:

```plaintext
source imageresizer/bin/activate
```

Great! Your Virtual Environment is ready to use.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675449004846/ee0e57b6-10db-4c9e-9da3-367743eabf13.webp align="left")

## Step 2: Select your Image

For this tutorial, I'm going to download a Photo by [Philip Jahn](https://unsplash.com/@pj_visual?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) from [Unsplash](https://unsplash.com/photos/RkU0A-yHQUo?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText).

It's a beautiful photo and looks like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675449024282/143cb87f-e86b-4bd1-b7fc-6398095a3cd7.webp align="center")

And as you can see, it's quite large:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675449035013/128b479d-caf7-4779-88e1-40a893a405e3.webp align="left")

5304x7592 is excellent for printing. Not so much for the web. So how can we resize this thing without making it look funny?

We can use OpenCV! Keep reading.

## Step 3: Install OpenCV

Next, we'll need to install OpenCV in our Virtual Environment.

Type in:

```plaintext
pip install opencv-python
```

And of course, I need to update pip. Just like it says, you type in:

```plaintext
pip install --upgrade pip
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675449045093/8366bae5-9532-453d-ad54-3c1fdf4af879.webp align="left")

Ok, now OpenCV is installed. Let's build our script.

## Step 4: Create a Python Script

Let's create the Python Script using OpenCV to resize the images.

The first line imports our library.

```Python
import cv2
```

Then we need to load the image we want to resize.

```Python
image = cv2.imread('original.jpg')
```

Now let's get the original dimensions of the image:

```Python
height, width = image.shape[:2]
```

This uses the shape function and stores the width and height of the image.

Now we need to set the output size of the image. This determines the size you want the *new* image to be.

```Python
output_size = (400, 400)
```

One thing to note, OpenCV will scale this. So the larger dimension of the image will be 400, and the smaller dimension will be scaled with a factor.

We need to calculate that scaling factor. Since the original dimensions are not square, resizing it to a square will distort the image. This will scale the smaller dimension appropriately.

```Python
scaling_factor = min(output_size[0]/width, output_size[1]/height)
```

This divides the original image dimensions by the intended dimensions, then picks the smaller result for your scaling factor.

Now, we'll resize the image. We'll pass in the original image, x and y scaling factors, and set interpolation to cv2.INTER\_AREA:

```Python
resized_image = cv2.resize(image, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
```

Finally, we'll write the file.

```Python
cv2.imwrite('output.jpg', resized_image)
```

Awesome. Let's run it!

## Step 5: Run the Script

At the prompt, type in:

```Python
python resize.py
```

If it's successful, you won't see any output.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675449059036/33688259-b29c-48eb-97b6-9529e2dc5bf7.webp align="left")

Now you can inspect your images. If you check the directory, you will see different file sizes, which tells you the image has changed:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675449067360/dd8bc789-48b4-4225-b445-72bf03216e2b.webp align="left")

Now, if we open the image, we can see it's 267x400! Quite a bit smaller.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675449074737/f3e94817-d83f-45a4-b444-d1b8b59188d9.webp align="left")

Congratulations! You just resized an image with OpenCV!

Here is the final code:

```Python
import cv2

# Load the image
image = cv2.imread('original.jpg')

# Get current image size
height, width = image.shape[:2]

# Set your output size
output_size = (400, 400)

# Set the scaling factor
scaling_factor = min(output_size[0]/width, output_size[1]/height)

# Resize the image in memory
resized_image = cv2.resize(image, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)

# Write it to a new file
cv2.imwrite('output.jpg', resized_image)
```

## Extra Credit

We can make this script more useful. Let's take in a parameter for the image name and the size we'd like to make it.

add the following line to the top:

```Python
import sys
```

Then, indent all the lines of code one tab.

Then add:

```Python
if len(sys.argv) > 1:
```

This statement says if arguments are passed to the command line, run the statements we just wrote to modify the image.

and change:

```Python
image = cv2.imread('original.jpg')
```

to

```Python
image = cv2.imread(sys.argv[1])
```

Then at the bottom of the script, add the following:

```Python
else:
    print("You must provide a filename of the original image")
```

Now when you run the script, you can put in any image name you want:

```plaintext
python resize.py original.jpg
```

If you don't enter a filename, the script will ask you to:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675449087231/7a6b0f59-f661-428e-a8f3-d5b5f01ae8bd.webp align="left")

There you go! Have fun playing around with this!

## Summary

In this tutorial, we learned how to resize images with OpenCV. It's pretty straightforward once you get the hang of it. It can get very complicated, however.

This is one of many tutorials I'll be doing to help you get started with OpenCV. Bookmark my blog and come back for more!

Questions? Comments? [Yell at me!](https://x.com/intent/follow?screen_name=JeremyCMorgan)
