Patrick
Patrick

Reputation: 867

.png image fail to do binary gray, while .jpg image can

Very easy code, I can read image from either image1 as png or image2 as jpg. Same image with different format.

Then filter out the darker part into black, show the brighter part into white.

#image = mpimg.imread('image1.png')
image = mpimg.imread('image2.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
thresh = (180, 255)
binary = np.zeros_like(gray)
binary[(gray > thresh[0]) & (gray <= thresh[1])] = 1

Somehow, when I plot the binary of image1, it is all black, but image2 looks what I tend to do. enter image description here

Upvotes: 0

Views: 357

Answers (1)

Steve Barnes
Steve Barnes

Reputation: 28380

The problem is most likely due to matplotlib.image successfully reading the png while the jpg falls back to using Pillow. The image resulting from the png read will be an array of floating point values with a range of 0.0 to 1.0 while the jpg read will be an array of bytes with values 0..255. As a result your clip operation will result in an all black image as everything is below 1.

See http://matplotlib.org/users/image_tutorial.html for more information.

Upvotes: 3

Related Questions