Skip to content Skip to sidebar Skip to footer

Convert 2d Array To Collored Image In Python

I have 2d list of ints like this: list1 = [[1, 30, 50], [21, 45, 9], [97, 321, 100]] Next i am going to convert this to numpy array: myarr = np.asarray(list1) Next i am going to

Solution 1:

The way to accomplish this is with numpy

import numpy as np
list1 = [[1, 30, 50], [21, 45, 9], [97, 321, 100]]
list1 = np.array(list1).reshape(-1, 3)

And now list1 will have the shape N x 3, where the 3 dimension is RGB. If you know the sizes of the final image, you can do

np.array(list1).reshape(N, M, 3)

and it will re-shape your array into RGB as you need. Then once you have the numpy array, you have your array in the shape of an image and can save it to PNG, etc.

Post a Comment for "Convert 2d Array To Collored Image In Python"