import numpy as np
from matplotlib.path import Path
w, h = 300, 200
mask = np.zeros((h, w))
verts = [(0, h), (w/2, 0), (w, h), (0, h)]
codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
path = Path(verts, codes)
mask[path.contains_points(mask)] = 1
result:
Traceback (most recent call last):
File "test.py", line 13, in <module>
mask[path.contains_points(mask)] = 1
File "C:\Python27\lib\site-packages\matplotlib\path.py", line 488, in contains_points
result = _path.points_in_path(points, radius, self, transform)
TypeError: Argument 0 to points_in_path must be an Nx2 numpy array
So matplotlib.path.Path.contains_points() does not expand array indexes which would be useful for mask creation.
How to mask numpy array with closed path (with matplotlib or without it if it's possible)