# Numpy Resize/Rescale Image

|

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

I would like to take an image and change the scale of the image, while it is a numpy array.

For example I have this image of a coca-cola bottle: bottle-1

Which translates to a numpy array of shape `(528, 203, 3)` and I want to resize that to say the size of this second image: bottle-2

Which has a shape of `(140, 54, 3)`.

How do I change the size of the image to a certain shape while still maintaining the original image? Other answers suggest stripping every other or third row out, but what I want to do is basically shrink the image how you would via an image editor but in python code. Are there any libraries to do this in numpy/SciPy?

👻 Read also: what is the best laptop for engineering students?

## Numpy Resize/Rescale Image resize: Questions

How do I resize an image using PIL and maintain its aspect ratio?

By saturdayplace

Is there an obvious way to do this that I"m missing? I"m just trying to make thumbnails.

526

Define a maximum size. Then, compute a resize ratio by taking `min(maxwidth/width, maxheight/height)`.

The proper size is `oldsize*ratio`.

There is of course also a library method to do this: the method `Image.thumbnail`.
Below is an (edited) example from the PIL documentation.

``````import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0] + ".thumbnail"
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(outfile, "JPEG")
except IOError:
print "cannot create thumbnail for "%s"" % infile
``````

526

This script will resize an image (somepic.jpg) using PIL (Python Imaging Library) to a width of 300 pixels and a height proportional to the new width. It does this by determining what percentage 300 pixels is of the original width (img.size[0]) and then multiplying the original height (img.size[1]) by that percentage. Change "basewidth" to any other number to change the default width of your images.

``````from PIL import Image

basewidth = 300
img = Image.open("somepic.jpg")
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
img.save("somepic.jpg")
``````

## Numpy Resize/Rescale Image resize: Questions

How to resize an image with OpenCV2.0 and Python2.6

I want to use OpenCV2.0 and Python2.6 to show resized images. I used and adopted this example but unfortunately, this code is for OpenCV2.1 and does not seem to be working on 2.0. Here my code:

``````import os, glob
import cv

ulpath = "exampleshq/"

for infile in glob.glob( os.path.join(ulpath, "*.jpg") ):
thumbnail = cv.CreateMat(im.rows/10, im.cols/10, cv.CV_8UC3)
cv.Resize(im, thumbnail)
cv.NamedWindow(infile)
cv.ShowImage(infile, thumbnail)
cv.WaitKey(0)
cv.DestroyWindow(name)
``````

Since I cannot use

``````cv.LoadImageM
``````

I used

``````cv.LoadImage
``````

instead, which was no problem in other applications. Nevertheless, cv.iplimage has no attribute rows, cols or size. Can anyone give me a hint, how to solve this problem?

194

If you wish to use CV2, you need to use the `resize` function.

For example, this will resize both axes by half:

``````small = cv2.resize(image, (0,0), fx=0.5, fy=0.5)
``````

and this will resize the image to have 100 cols (width) and 50 rows (height):

``````resized_image = cv2.resize(image, (100, 50))
``````

Another option is to use `scipy` module, by using:

``````small = scipy.misc.imresize(image, 0.5)
``````

There are obviously more options you can read in the documentation of those functions (cv2.resize, scipy.misc.imresize).

Update:
According to the SciPy documentation:

`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use `skimage.transform.resize` instead.

Note that if you"re looking to resize by a factor, you may actually want `skimage.transform.rescale`.

We hope this article has helped you to resolve the problem. Apart from Numpy Resize/Rescale Image, check other resize-related topics.

Want to excel in Python? See our review of the best Python online courses 2022. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:

Javier Schteiner

New York | 2022-12-01

Maybe there are another answers? What Numpy Resize/Rescale Image exactly means?. Will use it in my bachelor thesis

Walter Innsbruck

Singapore | 2022-12-01

I was preparing for my coding interview, thanks for clarifying this - Numpy Resize/Rescale Image in Python is not the simplest one. I am just not quite sure it is the best method

Anna Jackson

Massachussetts | 2022-12-01

I was preparing for my coding interview, thanks for clarifying this - Numpy Resize/Rescale Image in Python is not the simplest one. I just hope that will not emerge anymore

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries