python - Image.fromarray changes size - Stack Overflow
Image.fromarray() cannot handle 1-D arrays
python - How to read image from numpy array into PIL Image? - Stack Overflow
[np and PIL] how do you load images as array and then reshape back to view the image
PIL and numpy have different indexing systems. matrix[a, b] gives you the point at x position b, and y position a, but img.getpixel((a, b)) gives you the point at x position a, and y position b. As a result of this, when you are converting between numpy and PIL matrices, they switch their dimensions. To fix this, you could take the transpose (matrix.transpose()) of the matrix.
Here's what's happening:
import numpy as np
from PIL import Image
img = Image.new('L', (100, 28))
img.putpixel((5, 3), 17)
matrix = np.array(img)
print matrix[5, 3] #This returns 0
print matrix[3, 5] #This returns 17
matrix = matrix.transpose()
print matrix[5, 3] #This returns 17
print matrix[3, 5] #This returns 0
NumPy and PIL have different indexing systems. So a (100, 28) numpy array will be interpreted as an image with width 28 and height 100.
If you want a 28x100 image, then you should swap the dimensions for your image instantiation.
img = Image.new('L', (28, 100))
If you want a 100x28 image, then you should transpose the numpy array.
tmp = Image.fromarray(matrix.transpose())
More generally, if you're working with RGB, you can use transpose() to only swap the first two axes.
>>> arr = np.zeros((100, 28, 3))
>>> arr.shape
(100, 28, 3)
>>> arr.transpose(1, 0, 2).shape
(28, 100, 3)
arr = np.zeros((20,5000))
for idx,file in enumerate(images):
file_to_open = folder / file
print(file_to_open)
img = Image.open(file_to_open)
img = img.reduce(4)
print(img.size)
arr[idx]=np.asarray(img).reshape((1,5000))I have 19 images of 80 W x 61 H ( grayscale)I just want to stack 20x 5000 for machine learning.
examplebut problem occurs when I wanted to display only 1 image after the combine and I wasn't able to re-construct the image array to display the it as gif using the code below
Image.fromarray(arr[0].reshape((50,100),'L')
I think it didn't reshape properly..
Apologies if it's not entirely Python.
anyone familiar with PIL and reshape? Thanks