简体   繁体   中英

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 convert this to Image using PIL like this:

img = Image.fromarray(myarr, "I")
img.save("my.png")

the problem is that i dont want image in grayscale. I dont know how to convert this in to collored image. I have to use eny map function or somethig else ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM