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
tupleofslicesargument: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
Post a Comment