Skip to content

Instantly share code, notes, and snippets.

@Bahm
Last active February 27, 2017 04:15
Show Gist options
  • Save Bahm/3f175b5dadba2c214967b14aff6b7c7b to your computer and use it in GitHub Desktop.
Save Bahm/3f175b5dadba2c214967b14aff6b7c7b to your computer and use it in GitHub Desktop.
Construct convex hull from image
#!/usr/bin/env python3
from PIL import Image
from scipy.spatial import ConvexHull
import numpy as np
img = Image.open('image.png')
alphas = list(img.split()[-1].getdata())
region = np.argwhere(alphas)
region = [r[0] for r in region]
allVertices = [(i % img.width, int(i / img.width)) for i in region]
hull = ConvexHull(allVertices)
hullVertices = [allVertices[i] for i in hull.vertices]
print(str(hullVertices).strip('[]'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment