简体   繁体   中英

Converting numpy arrays to an image RGB

I have a list like this:

data = [(22, 21, 2), (217, 210, 166), (250, 240, 230), (276, 270, 260), (232, 234, 249), (274, 282, 270), (258, 266, 264)...]

The length is of 256 for an image 16 x 16.

I wanna convert it in the original image, but give me an image of 3*256 like this one:

例子

I´m using this code

img = Image.fromarray(data.astype('uint8'), 'RGB')

How can i fix this?

This is where np.reshape becomes useful.

What you need to do is:

Image.fromarray(np.reshape(image_arr, (16,16,3)).astype(np.uint8), 'RGB')

This will reshape your array into a 16x16x16 array (rank 3), which is what Image.fromarray expects.

For more information, here is the documentation for np.reshape

It might also be nice to use the reshape property of NDarrays and do image_arr.reshape((16,16,3)) instead.

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