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.

1

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.