Henrique Lee
Henrique Lee

Reputation: 59

Pillow .show() Not Displaying Correctly

I've been trying to get Pillow to display this 2-d array to get a grayscale image. The array is a 294 by 169 with values clamped between 0 and 255 and here's the code I'm using to display it

import numpy as np

from PIL import Image


def maxPool(arr,s):
    if (arr.shape[0] % s != 0 or arr.shape[1] % s != 0):
        print("Max pooling not compatible with image dimensions!")
        return

    rows = int(arr.shape[0] / s)
    cols = int(arr.shape[1] / s)

    nar = np.zeros([rows, cols])

    for i in range(rows):
        for j in range(cols):

            nar[i,j] = arr[i * s:i * s + s, j * s:j * s + s].max()

    return nar



#and then in the main method




arr = np.array(Image.open("dino.jpg")) #dino is a 588 x 338 image
arr2 = maxPool(arr,2)

i2 = Image.fromarray(arr,'L')

i2.show()

Somehow I get this image:

enter image description here

And yet when I try to display dino.jpg the program works fine! I've already checked the max pooling method quite thoroughly and I even checked the values in arr2 itself. Here's the first element:

[255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255. 255.
 255. 255. 255. 255. 255. 255. 255...,255]

So there should be no reason as to why it's showing black pixels in the top row!!

Upvotes: 0

Views: 114

Answers (1)

pippo1980
pippo1980

Reputation: 3031

here my attempt, work with me:


import numpy as np

from PIL import Image


def maxPool(arr,s):
    if (arr.shape[0] % s != 0 or arr.shape[1] % s != 0):
        print("Max pooling not compatible with image dimensions!")
        return

    rows = int(arr.shape[0] / s)
    cols = int(arr.shape[1] / s)

    nar = np.zeros([rows, cols], dtype= 'uint8')
    # nar = np.zeros([cols, rows])

    
    for i in range(rows):
        for j in range(cols):
            nar[i,j] = arr[i * s:i * s + s, j * s:j * s + s].max()
            # print(nar[i,j])

    return nar



#and then in the main method


pippo = Image.open("dino.jpg")

print(type(pippo), pippo.format, pippo.size, pippo.mode)


arr = np.array(pippo) #dino is a 588 x 338 image
arr2 = maxPool(arr,2)

i2 = Image.fromarray(arr2,'L')

i2.show()

solved changing nar = np.zeros([rows, cols]) to nar = np.zeros([rows, cols], dtype= 'uint8')

think you were trying to add floats64 to Image that doesnt support it or something like it

Upvotes: 1

Related Questions