'How to convert numpy array into image using PIL?
The following code gives me black images and I can't understand why:
Imports:
import numpy as np
from PIL import Image
Code:
arr2 = np.zeros((200,200), dtype=int)
arr2[80:120,80:120]=1
im = Image.fromarray(arr2,mode="1")
im.save("C:/Users/Admin/Desktop/testImage.jpg")
Solution 1:[1]
I think you want something more like this, using Boolean True and False:
import numpy as np
from PIL import Image
# Create black 1-bit array
arr2 = np.full((200,200), False, dtype=bool)
# Set some bits white
arr2[80:120,80:120]=True
im = Image.fromarray(arr2)
im.save('a.png')
print(im)
<PIL.Image.Image image mode=1 size=200x200 at 0x103FF2770>
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 | Mark Setchell |

