module: shapes.utils

shapes.utils.bbox_vertices(vertices)

Return bounding box of this object, i.e. (min x, min y, max x, max y)

Parameters:vertices – List [[x1, y1], [x2, y2]] or string "x1,y1,x2,y2,...,xn,yn"
shapes.utils.complex_polygon_area(vertices, triangles)

Returns the area of a complex polygon.

Parameters:
  • vertices – List [[x1, y1], [x2, y2]] or string "x1,y1,x2,y2,...,xn,yn"
  • triangles – List [[v1, v2, v3], [v1, v2, v3]] or string "v1,v2,v3,v1,v2,v3,...", where vx is an index into the vertices list.
Returns:

area as float

shapes.utils.create_shape_image_sample(shape, sample_width=256, sample_height=256)

Return a centered image sample of the shape, or None if a centered box intersects the shape edge

shapes.utils.get_dominant_image_colors(image, num_clusters=4)

Returns the dominant image color that isn’t pure white or black. Uses kmeans on the colors. Returns the result as RGB hex strings in the format [‘#rrggbb’, ‘#rrggbb’, ...].

Parameters:image – PIL image or path
shapes.utils.get_similar_shapes(bsdf, k=12, dE=20.0, dc=0.5)

Find k similar shapes to the given bsdf. The color must be within dE and the gloss contrast must be within dc.

Parameters:
  • bsdf – instance of bsdf.models.ShapeBsdfLabel_wd.
  • dE – tolerance for Lab color distance
  • dc – tolerance for gloss contrast
Returns:

list of shape.models.MaterialShape instances.

shapes.utils.mask_complex_polygon(image, vertices, triangles, bbox_only=False)

Crops out a complex polygon from an image. The returned image is cropped to the bounding box of the vertices and expanded with transparent pixels to a square.

Parameters:
  • image – path or PIL.Image
  • vertices – List [[x1, y1], [x2, y2]] or string "x1,y1,x2,y2,...,xn,yn"
  • triangles – List [[v1, v2, v3], [v1, v2, v3]] or string "v1,v2,v3,v1,v2,v3,...", where vx is an index into the vertices list.
  • bbox_only – if True, then only the bbox image is returned
Returns:

a tuple (masked PIL image, bbox crop PIL image), or None if the bbox is invalid

shapes.utils.mask_simple_polygon(image, vertices)

Crops out a polygon from an image

Parameters:
  • image – path or PIL.Image instance
  • vertices – List [[x1, y1], [x2, y2]] or string "x1,y1,x2,y2,...,xn,yn"
Returns:

a PIL.Image, or None if the bbox is invalid

shapes.utils.parse_segments(segments_str)

Parse segments stored as a string.

Parameters:
  • vertices – “v1,v2,v3,...”
  • return – [(v1,v2), (v3, v4), (v5, v6), ... ]
shapes.utils.parse_triangles(triangles_str)

Parse a list of vertices.

Parameters:vertices – “v1,v2,v3,...”
Returns:[(v1,v2,v3), (v4, v5, v6), ... ]
shapes.utils.parse_vertices(vertices_str)

Parse vertices stored as a string.

Parameters:
  • vertices – “x1,y1,x2,y2,...,xn,yn”
  • return – [(x1,y1), (x1, y2), ... (xn, yn)]
shapes.utils.polylines_from_segments(segments)

Group segments into nested polylines. Points are int indices into a vertex list. Segments can either be a flattened string, or a list of int tuples.

shapes.utils.render_full_complex_polygon_mask(vertices, triangles, width, height, inverted=False)

Returns a black-and-white PIL image (mode 1) of size width x height. The image is not cropped to the bounding box of the vertices. Pixels inside the polygon are 1 and pixels outside are 0 (unless inverted=True).

Parameters:
  • vertices – List [[x1, y1], [x2, y2]] or string "x1,y1,x2,y2,...,xn,yn"
  • triangles – List [[v1, v2, v3], [v1, v2, v3]] or string "v1,v2,v3,v1,v2,v3,...", where vx is an index into the vertices list.
  • width – width of the output mask
  • height – height of the output mask
  • inverted – if True, swap 0 and 1 in the output.
Returns:

PIL image of size (width, height)

shapes.utils.update_shape_dominant_delta(shape, save=True)

Update shape dominant_delta

shapes.utils.update_shape_dominant_rgb(shape, save=True)

Updates the dominant_* fields in a shape instance

shapes.utils.update_shape_image_crop(shape, save=True)

Update the cropped image for a shape

shapes.utils.update_shape_image_pbox(shape, padding=0.25, save=True)

Update the pbox image for a shape

shapes.utils.update_shape_label_pos(shape, save=True)
shapes.utils.update_shape_three(shape, save=True)