starLord073
starLord073

Reputation: 26

Convert an image into matrix of specific dimensions?

Is it possible to convert the image into an array of specific dimensions in python?

I have a group of images of different sizes. And I need them all to be exactly in a 50 X 50 matrix.

Is it possible to read an image via matplotlib preferably and then convert that array into a 50 X 50 array?

If it's possible, then how can I do it?

Upvotes: 0

Views: 977

Answers (1)

arnaud
arnaud

Reputation: 3473

Using Pillow library is what you want.

from PIL import Image
image_paths = ["image1.jpg", "image2.jpg"]
arrays = list()

for image_path in image_paths:
    img = Image.open(image_path)
    img.thumbnail(size=(50, 50))
    img_as_array = np.array(img)
    arrays.append(img_as_array)

Now arrays contains your images resized to (50, 50) as arrays!

Upvotes: 1

Related Questions