I've converted two images into numpy arrays
image1Array
image2Array
Both images have been converted to grayscale, so there are only 0
or 255
values.
In both examples, there are many rows of white at the top (and bottom):
[255 255 255 255 .... 255 255 255 255]
I believe what i'm referring to as 'row' is really an array. I'm new to using Numpy. So, there is an array for every line in the image, and every pixel in that line is represented with a 0
or 255
.
How do I find the first row that contains a black 0
pixel and the last row that contains a black 0
pixel? I should be able to use that to calculate the height. In these examples, this should be approximately the same number.
I believe numpy.where(image1Array == 0)[0]
is returning the row of every black pixel; min()
and max()
of that seems to be what i'm looking for, but i'm not sure yet.
Conversely, how do I find the width of each image? In these examples, Image 2
should have a larger width number than Image 1
EDIT
I think all I need is something like this:
Height (the difference between the first row with a black pixel and the last row with a black pixel):
(max(numpy.where(image1Array == 0)[0])) - (min(numpy.where(image1Array == 0)[0]))
Width (the difference between the lowest column value with a black pixel and the highest column value with a black pixel):
(max(numpy.where(image1Array == 0)[1])) - (min(numpy.where(image1Array == 0)[1]))
So far, my testing is showing this is correct. Comparing the two images in the example above, their heights are equal while image2Array's width is double that of image1Array.