Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How resize images when those converted to numpy array

Consider we only have images as.npy file. Is it possible to resizing images without converting their to images (because I'm looking for a way that is fast when run the code). for more info, I asked the way without converting to image, I have images but i don't want use those in code, because my dataset is too large and running with images is so slow, on the other hand, Im not sure which size is better for my imeges, So Im looking for a way that first convert images to npy and save .npy file and then preprocess npy file, for example resize the dimension of images.

like image 660
sha_hla Avatar asked Sep 25 '19 22:09

sha_hla


People also ask

Can an image could be converted into a NumPy array?

Using OpenCV Library to Convert images to NumPy array imread() function is used to load the image and It also reads the given image (PIL image) in the NumPy array format. Then we need to convert the image color from BGR to RGB. imwrite() is used to save the image in the file.

Can NumPy arrays be resized?

With the help of Numpy numpy. resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing.


1 Answers

Try PIL, maybe it's fast enough for you.

import numpy as np
from PIL import Image

arr = np.load('img.npy')
img = Image.fromarray(arr)
img.resize(size=(100, 100))

Note that you have to compute the aspect ratio if you want to keep it. Or you can use Image.thumbnail(), which can take an antialias filter.

There's also scikit-image, but I suspect it's using PIL under the hood. It works on NumPy arrays:

import skimage.transform as st

st.resize(arr, (100, 100))

I guess the other option is OpenCV.

like image 57
kwinkunks Avatar answered Nov 14 '22 22:11

kwinkunks