I have an 2d array consists of boolean values. For mode of fromarray both mode='1' and mode='L' not working properly. Both of them returns an black image while it also should have white values for where indexes point to True. How can I get the proper image?
import numpy as np
from PIL import Image
maskArr = np.array(arr) * 255
print(maskArr.shape) # Returns (600, 800)
print(np.where(maskArr == True)) # Returns a tuple of index of True values.
maskImg1 = Image.fromarray(maskArr, mode='1')
maskImg2 = Image.fromarray(maskArr, mode='L')
maskImg1.save("./maskImg.jpg")
maskImg2.save("./maskImg2.jpg")
You want to ensure that maskArr has dtype uint8
, and that you pass mode='L'
to PIL:
maskArr = np.array(arr, dtype=np.uint8) * 255
maskImg = Image.fromarray(maskArr, mode='L')
maskImg.save("./maskImg.jpg")
Currently, your maskArr
has dtype int64
, so PIL doesn't know to treat 255 as white and 0 as black.
When I run this code with arr = [[True, False]*100,[False, True]*100]*100
, I get this output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.