[Python] Converting image to black and white and use it as array

Discussão em 'Python' iniciado por Stack, Agosto 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to convert a colored image to a black and white one.

    The original image is the following:

    [​IMG]

    I have a few problems. First:

    import pylab as pl
    import Image

    im = Image.open('joconde.png')

    pl.imshow(im)
    pl.axis('off')
    pl.show()


    I get this:

    [​IMG]

    Why is it rotated? That's not the point but I'd like to know why.

    im_gray = im.convert('1')

    pl.imshow(im_gray)
    pl.show()


    And here is the processed black and white image:

    [​IMG]

    Now everything looks working. But I need to use that image as a numpy array in order to do some image processing. All I have to do is this:

    import numpy as np

    im_arr = np.array(im_gray)

    pl.imshow(im_arr)
    pl.axis('off')
    pl.show()


    But I get this:

    [​IMG]

    Why is this happening? I tried also:

    im_arr = np.array(im_gray, dtype='float')


    or:

    im_arr = np.asarray(im_gray)


    But nothing seems working. Maybe the problem is in the show method but I don't know.

    Continue reading...

Compartilhe esta Página