python - Indexing a 2D numpy array inside a function using a function parameter -


say have 2d image in python stored in numpy array , called npimage (1024 times 1024 pixels). define function showimage, take paramater slice of numpy array:

def showimage(npimage,slicenumpy):     imshow(npimage(slicenumpy)) 

such can plot given part of image, lets say:

showimage(npimage,[200:800,300:500]) 

would plot image lines between 200 , 800 , columns between 300 , 500, i.e.

imshow(npimage[200:800,300:500]) 

is possible in python? moment passing [200:800,300:500] parameter function result in error.

thanks or link. greg

it's not possible because [...] syntax error when not directly used slice, do:

  • give relevant sliced image - not seperate argument showimage(npimage[200:800,300:500]) (no comma)

  • or give tuple of slices argument: showimage(npimage,(slice(200,800),slice(300:500))). can used slicing inside function because way of defining slice:

    npimage[(slice(200,800),slice(300, 500))] == npimage[200:800,300:500] 

a possible solution second option be:

import matplotlib.pyplot plt def showimage(npimage, slicenumpy):     plt.imshow(npimage[slicenumpy])     plt.show()  showimage(npimage, (slice(200,800),slice(300, 500))) # plots relevant slice of array. 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -