Reputation: 5282
I have manipulated a jpg image so that I have foreground isolated, and all black pixels everywhere else. I'd like to be able to crop the image so that there are no full black rows above and below, or full black columns left and right of the foreground. I'm thinking I could get the 4 indices I need just looping through the Numpy array, but was wondering if there is a more straightforward and/or fast approach.
import numpy as np
import matplotlib.pyplot as plt
im=np.array(
[[0,0,0,0,0,0,0],
[0,0,1,1,1,0,0],
[0,1,1,1,0,0,0],
[0,0,1,1,0,0,0],
[0,0,0,0,0,0,0]])
plt.imshow(im, cmap=plt.cm.gray,interpolation='nearest')
Then something happens here and I get:
Upvotes: 0
Views: 568
Reputation: 12689
im[~np.all(im == 0, axis=1)]
can remove the rows with all zero. axis=2
will be the columns deletion. Would that work for you?
Upvotes: 1