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:
JavaScript
x
8
1
import numpy as np
2
from PIL import Image
3
4
benign = Image.open("./benign.png")
5
benign = np.array(benign)
6
7
print(benign) ### Debug here!
8
The Python version is 3.8.12.
Advertisement
Answer
You are looking for the np function astype()
(documentation).
Use it to cast the booleans to integers:
JavaScript
1
8
1
import numpy as np
2
from PIL import Image
3
4
benign = Image.open("./benign.png")
5
benign = np.array(benign)
6
new_benign = benign.astype(int)
7
print(new_benign)
8