python - How to, given a 2D numpy arrayMatrix that contains "triplets" of RGB values generate an image? -


you see, of posts discuss image creation deal 3d matrix[0][1][2] contains necessary info apply directly

img = image.fromarray(matrix, 'rgb') 

however, i'm stuck massive matrix has "3n" columns , "n" rows. might see, "image" coded in fashion reminding me of p3/p6 formats:

[ 0 0 0 255 255 255 0 0 0   0 0 0 255 255 255 0 0 0    255 255 255 0 0 0 255 255 255] 

the above 2d matrix represents 3x3 "pixels", , using image.fromarray produces image full of holes. i've thinking of splitting 3 (!!!) 2 dimensional arrays , using np.dstack sounds terribly inefficient code dinamically generating thousands of matrices large dimensions (700x2100) need presented images.

this i'm thinking of doing, btw:

r = np.zeros((y, x), dtype = np.uint8) # same g & b     row in range(y):         column in range(3*x):             if column % 3 == 0: # column-1 g , -2 b                 r[row][column/3] = 2dmatrix[row][column] #after populating r, g, b rgb0 = np.dstack([r, g, b]) img = image.fromarray(rgb0, 'rgb') 

thanks!

numpy.reshape should work this. it's important color values 8-bit unsigned:

>>> import numpy np  >>> = [[0, 0, 0, 255, 255, 255, 0, 0, 0], ...      [0, 0, 0, 255, 255, 255, 0, 0, 0], ...      [255, 255, 255, 0, 0, 0, 255, 255, 255]] >>> = np.array(a) >>> a.astype('u1').reshape((3,3,3))  array([[[  0,   0,   0],         [255, 255, 255],         [  0,   0,   0]],         [[  0,   0,   0],         [255, 255, 255],         [  0,   0,   0]],         [[255, 255, 255],         [  0,   0,   0],         [255, 255, 255]]], dtype=uint8)  >>> import pil.image >>> = pil.image.fromarray(a.astype('u1').reshape((3,3,3)), 'rgb') 

this seems work way expect:

>>> i.size (3, 3) >>> i.getpixel((0,0)) (0, 0, 0) >>> i.getpixel((1,0)) (255, 255, 255) >>> i.getpixel((2,0)) (0, 0, 0) >>> i.getpixel((0,1)) (0, 0, 0) >>> i.getpixel((1,1)) (255, 255, 255) >>> i.getpixel((2,1)) (0, 0, 0) >>> i.getpixel((0,2)) (255, 255, 255) >>> i.getpixel((1,2)) (0, 0, 0) >>> i.getpixel((2,2)) (255, 255, 255) 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -