9951 explained code solutions for 126 technologies


python-scipyHow can I use Python and SciPy to downsample an image?


Using Python and SciPy, you can downsample an image by first loading it into an array using SciPy's imread() function. Then, use SciPy's resize() function to downsize the image.

Example code

from scipy.misc import imread, imsave, imresize

# Read an JPEG image into a numpy array
img = imread('assets/cat.jpg')

# Resize the image
img_resized = imresize(img, (300, 300))

# Save the resized image
imsave('assets/cat_resized.jpg', img_resized)

The above code will read the image cat.jpg from the assets folder, resize it to 300x300 pixels, and save it as cat_resized.jpg in the assets folder.

Code explanation

  1. imread(): Loads the image into a numpy array
  2. imresize(): Resizes the image
  3. imsave(): Saves the resized image

Helpful links

Edit this code on GitHub