Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Images Have Grey Values of True and False

I’m planning to process some images using Pycharm. However, I find a bug and start to find the reason. Finally, I find that the images have grey values of True and False, but they should be 1 and 0, is there any way to change it?

The image is generated in Pycharm using:

import numpy as np
from PIL import Image

benign = Image.open("./benign.png")
benign = np.array(benign)

print(benign) ### Debug here!

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The python version is 3.8.12. Thanks for everyone in advance!

>Solution :

You are looking for the np function astype() (documentation).
Use it to cast the booleans to integers:

import numpy as np
from PIL import Image

benign = Image.open("./benign.png")
benign = np.array(benign)
new_benign = benign.astype(int)
print(new_benign)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading