Advertisement
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.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Python help

  • 18-03-2017 12: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