We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

n3x on 11/11/07


Tagged


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

copyleft


prototype for auto-generated soup user icons


Published in: Python 


URL: http://dev.soup.io/post/281433

See link for screenshot

  1. colormode(HSB)
  2.  
  3. class Icon(object):
  4. def __init__(self):
  5. self.width = 5
  6. self.height = 5
  7. self.pixels = [random(2) \
  8. for i in xrange(self.width * self.height)]
  9. self._symmetrize()
  10. self.pixelsize = 8
  11. self.fg_col = (random(1.0), random(0.4, 1.0), 0.25)
  12. self.bg_col = ((self.fg_col[0] + 0.2) % 1.0, self.fg_col[1], 1.0)
  13.  
  14. def paint(self, x_pos=0, y_pos=0):
  15. nostroke()
  16. for y in xrange(self.height):
  17. for x in xrange(self.width):
  18. if self.pixels[x + self.width * y]:
  19. col = self.fg_col
  20. else:
  21. col = self.bg_col
  22. fill(color(col))
  23. rect(x * self.pixelsize + x_pos, y * self.pixelsize + y_pos,
  24. self.pixelsize, self.pixelsize)
  25.  
  26. def _symmetrize(self):
  27. for y in xrange(self.height):
  28. for x in xrange(int(self.width / 2), self.width):
  29. self.pixels[x + self.width * y] = \
  30. self.pixels[(self.width - (x + 1)) + self.width * y]
  31.  
  32.  
  33. class IconGrid(object):
  34. def __init__(self, width, height):
  35. self.width = width
  36. self.height = height
  37. self.icons = [Icon() for x in xrange(self.width) \
  38. for y in xrange(self.height)]
  39.  
  40. def paint(self):
  41. for y in xrange(self.height):
  42. for x in xrange(self.width):
  43. icon = self.icons[x + y * self.width]
  44. pw = icon.width * (icon.pixelsize + 1)
  45. ph = icon.height * (icon.pixelsize + 1)
  46. icon.paint(x * pw, y * ph)
  47.  
  48. grid = IconGrid(8, 13)
  49. grid.paint()

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: n3x on December 12, 2007

This is the entire code you need -- iff you run it in NodeBox ... I'm planning to post an RMagick version soon, so non-Mac users can play with it, too ;-)

You need to login to post a comment.