Last active
November 4, 2015 22:24
-
-
Save rdenadai/b88dc564fb996b5cb3ef to your computer and use it in GitHub Desktop.
This gist uses Pillow to get the boundary edges of an image and generate the polygon position of the draw inside. Im using this script to get the polygon of my games sprites to create the collisions mechanics using libgdx!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageFilter | |
filename = "dship1" | |
image = Image.open(filename + ".png") | |
image = image.filter(ImageFilter.FIND_EDGES) | |
image.save(filename + "_edge.png") | |
cols = image.width | |
rows = image.height | |
points = [] | |
w = 1 | |
h = 1 | |
i = 0 | |
for pixel in list(image.getdata()): | |
if pixel[3] > 0: | |
points.append((w, h)) | |
if i == cols: | |
w = 0 | |
i = 0 | |
h += 1 | |
w += 1 | |
i += 1 | |
with open(filename + "_points.txt", "wb") as nf: | |
nf.write(',\n'.join('%s, %s' % x for x in points)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment