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
imread()
: Loads the image into a numpy arrayimresize()
: Resizes the imageimsave()
: Saves the resized image
Helpful links
- SciPy Documentation: https://docs.scipy.org/doc/scipy/reference/
- SciPy Image Processing: https://scipy-lectures.org/advanced/image_processing/
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I create a zero matrix using Python and Numpy?
- How do I use the NumPy transpose function in Python?
- How do I create a numpy array of zeros using Python?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use Python and SciPy to create a tutorial PDF?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use the scipy ttest_ind function in Python?
- How do I use the trapz function in Python SciPy?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
See more codes...