I am having an array of rgb tuples
array = [(144, 144, 133), (85, 87, 75), (140, 87, 70), (129, 107, 105), (129, 107, 105), (194, 179, 171), (178, 164, 159), (100, 105, 122), (36, 38, 57), (59, 48, 49), (59, 48, 49), (152, 149,
148), (152, 149, 148), (0, 0, 0), (0, 0, 0), (98, 81, 84)...]
which i would like make a square shape and change to a PIL image.
dt = np.dtype('int,int,int')
array2d = np.array(array, dt)
im_pil = Image.fromarray(obj=array2d.reshape(int(math.sqrt(size)), int(math.sqrt(size))), mode='RGB')
data = io.BytesIO()
im_pil.save(data, "bmp")
for some unknown for me reason, it separetes each number e.g. (144,144,133) -> (144,0,0), (0,144,0), (0,0,133), (0,0,0)
Is my problem the shape of initial array? Or the modification around it? Any ideas? of course i tried np.array(array, np.uint8) same problem
Thanks in advance.
CodePudding user response:
I beleive, thatour problem lies in your reshape function. You reshape your array into an 2d array with shape [sqrt(size),sqrt(size)]
. What you need instead is a 3d array with shape [sqrt(size),sqrt(size),3]
(x dimension, y dimension, 3 RGB values).
To solve your problem you should do
im_pil = Image.fromarray(obj=array2d.reshape(int(math.sqrt(size)), int(math.sqrt(size)),3), mode='RGB')
CodePudding user response:
instead of this:
im_pil = Image.fromarray(obj=array2d.reshape(int(math.sqrt(size)), int(math.sqrt(size))), mode='RGB')
Try this:
arr3d = arr.reshape(2,3,2)
Image.fromarray(arr3d,'RGB').show()