PIL's Image.fromarray function() confused me

Viewed 313

I want to create a small image with PIL, my idea is first creating an ndarray object with numpy, then transforming it into a Image object, but it doesn't work!

small = np.array([[0, 1, 1, 0, 1, 0],
        [0, 1, 1, 0, 1, 0]])
small = Image.fromarray(small, 'L')
print(small.size)

these codes print (6, 2), so why it transposes my original input? What made my even more confused is that when I try to print all the pixels:

for i in range(6):
    for j in range(2):
        print(small.getpixel((i, j)), end='')

it prints out : 0 0 0 0 0 1 0 0 0 0 0 0 I had no idea about what have happened ........

1 Answers
Image.fromarray(small,'L')

expects an 8 bit Input? converting the Input Array to np.int8 works for me..

small = np.array([[0, 1, 1, 0, 1, 0],
        [0, 1, 1, 0, 1, 0]],dtype=np.int8)

Just removing the 'L' will also work

Related