'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!
The Python version is 3.8.12.
Solution 1:[1]
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)
Solution 2:[2]
try:
benign = 1*np.array(benign)
this should convert True to 1 and False to 0.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Damiaan |
| Solution 2 | warped |

