3

I have a 3D numpy array, which is a stack of 100 300x300 images. I want to resize all the images in the stack to be 200x200. I tried to use the numpy resize function:

import numpy as np

img_stack_sm = np.resize(img_stack, (100, 200, 200))

...but doing so scrambles the images (as shown by plotting). How can this be done in one pass? Thank you.

2
  • numpy resize just chops off values. Read its docs. You want an image resizing that interpolates new values or resamples.
    – hpaulj
    Feb 25, 2017 at 2:57
  • How would you resize one image?
    – hpaulj
    Feb 25, 2017 at 3:21

1 Answer 1

5

I just used a for loop in the end and cv2:

import cv2

width = 200
height = 200
img_stack_sm = np.zeros((len(img_stack), width, height))

for idx in range(len(img_stack)):
    img = img_stack[idx, :, :]
    img_sm = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
    img_stack_sm[idx, :, :] = img_sm
1
  • You should use np.zeros((len(img_stack), height, width)) as Numpy works per rows, while OpenCV wants classic width x height.
    – decadenza
    Apr 28, 2021 at 15:00

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.