TisButaScratch
TisButaScratch

Reputation: 173

Create image from RGB list python PIL

I have a list that represents the the RGB values of a 360x640pixel image, the image list is in the following format img_list[y_pix][x_pix][R,G,B]. so for a 2x2pixel which has white pixels in the top left and bottom right, and black in the reverse my list would be [[[255,255,255],[0,0,0]][[0,0,0],[255,255,255]]].

I have tried passing this to PIL as an array

image_array = np.asarray(frame_array)
img = PIL.Image.fromarray(image_array)

Upvotes: 1

Views: 800

Answers (1)

javidcf
javidcf

Reputation: 59681

You need to specify np.uint8 data type so PIL understand that each value is a byte in the RGB representation.

import PIL
import numpy as np

l = [[[255, 255, 255], [0, 0, 0]], [[0, 0, 0], [255, 255, 255]]]
image_array = np.array(l, dtype=np.uint8)
img = PIL.Image.fromarray(image_array)
img.show()

Output (magnified):

Output

Upvotes: 1

Related Questions