Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antiface/6b8342c27791dd90661deff5de0628da to your computer and use it in GitHub Desktop.
Save antiface/6b8342c27791dd90661deff5de0628da to your computer and use it in GitHub Desktop.
Generate vectorpixels based on bitmaps (2 color pictures).
#!/usr/bin/python
""" Generates vectorpixels based on bitmaps (2 color pictures).
TODO: use element tree for XML; implement Floyd-Steinberg
dithering for color and greyscale images; implement vertical
and horiontal scanlines """
import Image
from sys import argv
class vectorpixel:
def __init__(self, image):
self.i = Image.open(image)
self.px = self.i.load()
def construct(self, grid=24, line=1, rounded=4, test=(lambda x: x == 0)):
self.grid = grid
self.line = line
self.rounded = rounded
self.width = self.height = self.grid - 2 * self.line
self.test = test
self.fill = '#000000'
self.constructed = True
def _yieldlocations(self):
for y in range(self.i.size[0]):
for x in range(self.i.size[1]):
if self.test(self.px[x,y]):
yield (x,y)
def _mkelements(self):
for l in self._yieldlocations:
yield "<rect x='%s' y='%s' width='%s' height='%s' rx='%s' fill='%s'/>" % (
self.grid * l[0] + self.line, self.grid * l[1] + self.line, self.width, self.height, self.rounded, self.fill)
def _format(self):
output = '<svg xmlns="http://www.w3.org/2000/svg" width="%s" height="%s">\n' % (self.i.size[0] * self.grid, self.i.size[1] * self.grid)
for e in _mkelements:
output += e
output += '\n'
output += '</svg>'
return output
def generate(self):
if not self.constructed:
self.construct()
return self._format()
if __name__ == "__main__":
if len(argv) != 2:
print "usage: python vectorpixels.class.py inputimage > output.svg"
else:
v = vectorpixel(argv[1])
print v.generate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment