Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resize image of a 4-dimensional array in python

I'm working with Python 3.

I have a variable data from type unit8 and size (60000, 1, 28, 28), what means, that I have 60000 images with 1 channel and a size of 28x28 pixels.
Now I like to resize the images to 60x60 pixels, so that I get a size of (60000, 1, 60, 60)

Can someone help me?

like image 911
csi Avatar asked Mar 17 '26 00:03

csi


1 Answers

Following solution worked for me. After list comprehension the list will be stacked back to a batch with numpy:

import cv2
import numpy as np

target_shape = (60, 60)

def _resize_image(image, target):
   return cv2.resize(image, dsize=(target[0], target[1]), interpolation=cv2.INTER_LINEAR)

image = [_resize_image(image=i, target=target_shape) for i in data]
data_batch = np.stack(image, axis=0)
like image 93
Michael Gann Avatar answered Mar 19 '26 13:03

Michael Gann