1

I need to load an image, convert it into a np.array and then delete all rows that contains all zeros. For load the image I use:

image = Image.open(path).convert(mode='L')  
image = (np.array(image,dtype=int))

The image is a B/W image where the black is 0 and the white is 1. The image is like:

0000000000100000000001000000
0000000000000000010000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000000000000000000000000000
0000010011000000001110000000

And I need to delete all zeros row to get something like:

0000000000100000000001000000
0000000000000000010000000000
0000010011000000001110000000

Is there a numpy function to do this? Am I doing it in the right way? Thank you so much.

Matteo_Sid
  • 252
  • 1
  • 2
  • 11
  • 3
    Does this answer your question? [remove zero lines 2-D numpy array](https://stackoverflow.com/questions/11188364/remove-zero-lines-2-d-numpy-array) – sparaflAsh Dec 23 '19 at 14:05

1 Answers1

0

I would do the following:

new_image = []

for i in range(image.shape[1]):
    if 1 in image[i,:]:
        new_image.append(image[i,:])
    else:
        continue

You can then convert back to a numpy array just using

new_image = np.array(new_image)
Luismi98
  • 282
  • 3
  • 14