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
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.

Python help

  • 18-03-2017 01:43PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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