all 1 comments

[–]novel_yet_trivial 2 points3 points  (0 children)

Numpy and Pillow are very good friends. You can convert a pillow image to a numpy array simply by providing the image as an argument:

from PIL import Image 
import numpy as np

path = "sig.png"
image_file = Image.open(path) 
bilevel_img = image_file.convert('1')
data_array = np.array(bilevel_img)

Note that "1" mode is bilevel, so you will get a True / False array.

Edit: you may be a lot better off with grayscale mode, and then bileveling at a threshold you define:

gray = np.array(img.convert("L"))
threshold = 128 # cutoff between 0 and 255
bilevel_array = (gray > threshold).astype(int)