Skip to content
Advertisement

How can I draw lines into numpy arrays?

I would like to be able to draw lines into numpy arrays to get off-line features for on-line handwriting recognition. This means I don’t need the image at all, but I need for some positions in a numpy array who an image of a given size would look like.

I would like to be able to specify an image size and then draw strokes like this:

import module
im = module.new_image(width=800, height=200)
im.add_stroke(from={'x': 123, 'y': 2}, to={'x': 42, 'y': 3})
im.add_stroke(from={'x': 4, 'y': 3}, to={'x': 2, 'y': 1})
features = im.get(x_min=12, x_max=15, y_min=0, y_max=111)

Is something simple like that possible (preferably directly with numpy / scipy)?

(Please note that I want grey-scale interpolation. So features should be a matrix of values in [0, 255].)

Advertisement

Answer

Thanks to Joe Kington for the answer! I was looking for skimage.draw.line_aa.

import scipy.misc
import numpy as np
from skimage.draw import line_aa
img = np.zeros((10, 10), dtype=np.uint8)
rr, cc, val = line_aa(1, 1, 8, 4)
img[rr, cc] = val * 255
scipy.misc.imsave("out.png", img)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement