Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Python help

Options
  • 18-03-2017 1:43pm
    #1
    Registered Users Posts: 141 ✭✭


    I am new to Python and have to use it for a college assignment.

    I am trying to execute a section of code that will import a jpg image using PIL module and convert it to a numpy array.

    I keep getting the issue of "IndexError: too many indices for array"

    Been looking around on-line on how to fix this but none the wiser.

    import numpy as np
    import matplotlib.pylab as pyp

    from PIL import Image
    empire = np.array(Image.open('./empire.jpg'))
    print empire.shape, empire.dtype
    print empire [:2,:2]

    pyp.figure('Empire State Building')
    pyp.imshow(empire)
    pyp.show()

    IndexError Traceback (most recent call last)
    <ipython-input-4-391414696018> in <module>()
    6 empire = np.array(Image.open('./empire.jpg'))
    7 print empire.shape, empire.dtype
    ----> 8 print empire [:2,:2]
    9
    10 pyp.figure('Empire State Building')

    IndexError: too many indices for array


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Look at the shape of the array.

    print empire.shape

    Run the code interactively (via notebook or debugger) so you can see what is happening.


Advertisement