pixelarray.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python
  2. import os, pygame
  3. from pygame.compat import xrange_
  4. main_dir = os.path.split(os.path.abspath(__file__))[0]
  5. data_dir = os.path.join(main_dir, 'data')
  6. def show (image):
  7. screen = pygame.display.get_surface()
  8. screen.fill ((255, 255, 255))
  9. screen.blit (image, (0, 0))
  10. pygame.display.flip ()
  11. while 1:
  12. event = pygame.event.wait ()
  13. if event.type == pygame.QUIT:
  14. raise SystemExit
  15. if event.type == pygame.MOUSEBUTTONDOWN:
  16. break
  17. def main():
  18. pygame.init ()
  19. pygame.display.set_mode ((255, 255))
  20. surface = pygame.Surface ((255, 255))
  21. pygame.display.flip ()
  22. # Create the PixelArray.
  23. ar = pygame.PixelArray (surface)
  24. r, g, b = 0, 0, 0
  25. # Do some easy gradient effect.
  26. for y in xrange_ (255):
  27. r, g, b = y, y, y
  28. ar[:,y] = (r, g, b)
  29. del ar
  30. show (surface)
  31. # We have made some gradient effect, now flip it.
  32. ar = pygame.PixelArray (surface)
  33. ar[:] = ar[:,::-1]
  34. del ar
  35. show (surface)
  36. # Every second column will be made blue
  37. ar = pygame.PixelArray (surface)
  38. ar[::2] = (0, 0, 255)
  39. del ar
  40. show (surface)
  41. # Every second row will be made green
  42. ar = pygame.PixelArray (surface)
  43. ar[:,::2] = (0, 255, 0)
  44. del ar
  45. show (surface)
  46. # Manipulate the image. Flip it around the y axis.
  47. surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp'))
  48. ar = pygame.PixelArray (surface)
  49. ar[:] = ar[:,::-1]
  50. del ar
  51. show (surface)
  52. # Flip the image around the x axis.
  53. ar = pygame.PixelArray (surface)
  54. ar[:] = ar[::-1,:]
  55. del ar
  56. show (surface)
  57. # Every second column will be made white.
  58. ar = pygame.PixelArray (surface)
  59. ar[::2] = (255, 255, 255)
  60. del ar
  61. show (surface)
  62. # Flip the image around both axes, restoring it's original layout.
  63. ar = pygame.PixelArray (surface)
  64. ar[:] = ar[::-1,::-1]
  65. del ar
  66. show (surface)
  67. # Rotate 90 degrees clockwise.
  68. w, h = surface.get_size ()
  69. surface2 = pygame.Surface ((h, w), surface.get_flags (), surface)
  70. ar = pygame.PixelArray (surface)
  71. ar2 = pygame.PixelArray (surface2)
  72. ar2[...] = ar.transpose ()[::-1,:]
  73. del ar, ar2
  74. show (surface2)
  75. # Scale it by throwing each second pixel away.
  76. surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp'))
  77. ar = pygame.PixelArray (surface)
  78. sf2 = ar[::2,::2].make_surface ()
  79. del ar
  80. show (sf2)
  81. # Replace anything looking like the blue color from the text.
  82. ar = pygame.PixelArray (surface)
  83. ar.replace ((60, 60, 255), (0, 255, 0), 0.06)
  84. del ar
  85. show (surface)
  86. # Extract anything which might be somewhat black.
  87. surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp'))
  88. ar = pygame.PixelArray (surface)
  89. ar2 = ar.extract ((0, 0, 0), 0.07)
  90. sf2 = ar2.surface
  91. del ar, ar2
  92. show (sf2)
  93. # Compare two images.
  94. surface = pygame.image.load (os.path.join (data_dir, 'alien1.gif'))
  95. surface2 = pygame.image.load (os.path.join (data_dir, 'alien2.gif'))
  96. ar1 = pygame.PixelArray (surface)
  97. ar2 = pygame.PixelArray (surface2)
  98. ar3 = ar1.compare (ar2, 0.07)
  99. sf3 = ar3.surface
  100. del ar1, ar2, ar3
  101. show (sf3)
  102. if __name__ == '__main__':
  103. main()