#!/usr/bin/env python """ Generates vectorpixels based on 2-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 SOURCEIMAGE = 'attention.png' class vectorpixel: def __init__(self, image): self.i = Image.open(image) self.px = self.i.load() self.constructed = False 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 x in range(self.i.size[0]): for y 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 "" % ( self.grid * l[0] + self.line, self.grid * l[1] + self.line, self.width, self.height, self.rounded, self.fill) def _format(self): output = '\n' % (self.i.size[0] * self.grid, self.i.size[1] * self.grid) for e in self._mkelements(): output += e output += '\n' output += '' return output def generate(self): if not self.constructed: self.construct() return self._format() if __name__ == "__main__": v = vectorpixel(SOURCEIMAGE) print v.generate()