Skip to content

png2poly

png2poly(filename)

Export polylines from png image

Reads a png file and outputs a list of polylines that constitute the contours of the png, using marching squares. This is useful for generating 2D "realworld" data.

Parameters:

Name Type Description Default
filename str

Path to png file

required

Returns:

Name Type Description
poly list of numpy double arrays

Each list element is a matrix of ordered polyline vertex coordinates

Notes

This often results in "duplicate" polylines (one is the white->black contour, other is the black->white contour.

Examples:

TODO

Source code in src/gpytoolbox/png2poly.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def png2poly(filename):
    """Export polylines from png image

    Reads a png file and outputs a list of polylines that constitute the contours of the png, using marching squares. This is useful for generating 2D "realworld" data. 

    Parameters
    ----------
    filename : str
        Path to png file

    Returns
    -------
    poly : list of numpy double arrays
        Each list element is a matrix of ordered polyline vertex coordinates

    Notes
    -----
    This often results in "duplicate" polylines (one is the white->black contour, other is the black->white contour.

    Examples
    --------
    TODO
    """
    polypic = imread(filename)
    # For some reason reading the image flips it by 90 degrees. This fixes it
    polypic = rotate(polypic, angle=-90, resize=True)
    # print(polypic)

    # convert to greyscale and remove alpha if neccessary
    if len(polypic.shape)>2:
        if polypic.shape[2]==4:
            polypic = rgb2gray(rgba2rgb(polypic))
        elif polypic.shape[2]==3:
            polypic = rgb2gray(polypic)
    # find contours
    polypic = polypic/np.max(polypic)
    poly = measure.find_contours(polypic, 0.5)
    return poly