arraydemo.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python
  2. import os
  3. import pygame
  4. from pygame import surfarray
  5. from pygame.locals import *
  6. main_dir = os.path.split(os.path.abspath(__file__))[0]
  7. def surfdemo_show(array_img, name):
  8. "displays a surface, waits for user to continue"
  9. screen = pygame.display.set_mode(array_img.shape[:2], 0, 32)
  10. surfarray.blit_array(screen, array_img)
  11. pygame.display.flip()
  12. pygame.display.set_caption(name)
  13. while 1:
  14. e = pygame.event.wait()
  15. if e.type == MOUSEBUTTONDOWN: break
  16. elif e.type == KEYDOWN and e.key == K_s:
  17. #pygame.image.save(screen, name+'.bmp')
  18. #s = pygame.Surface(screen.get_size(), 0, 32)
  19. #s = s.convert_alpha()
  20. #s.fill((0,0,0,255))
  21. #s.blit(screen, (0,0))
  22. #s.fill((222,0,0,50), (0,0,40,40))
  23. #pygame.image.save_extended(s, name+'.png')
  24. #pygame.image.save(s, name+'.png')
  25. #pygame.image.save(screen, name+'_screen.png')
  26. #pygame.image.save(s, name+'.tga')
  27. pygame.image.save(screen, name+'.png')
  28. elif e.type == QUIT:
  29. raise SystemExit()
  30. def main(arraytype=None):
  31. """show various surfarray effects
  32. If arraytype is provided then use that array package. Valid
  33. values are 'numeric' or 'numpy'. Otherwise default to NumPy,
  34. or fall back on Numeric if NumPy is not installed.
  35. """
  36. if arraytype not in ('numpy', None):
  37. raise ValueError('Array type not supported: %r' % arraytype)
  38. import numpy as N
  39. from numpy import int32, uint8, uint
  40. pygame.init()
  41. print ('Using %s' % surfarray.get_arraytype().capitalize())
  42. print ('Press the mouse button to advance image.')
  43. print ('Press the "s" key to save the current image.')
  44. #allblack
  45. allblack = N.zeros((128, 128), int32)
  46. surfdemo_show(allblack, 'allblack')
  47. #striped
  48. #the element type is required for N.zeros in NumPy else
  49. #an array of float is returned.
  50. striped = N.zeros((128, 128, 3), int32)
  51. striped[:] = (255, 0, 0)
  52. striped[:,::3] = (0, 255, 255)
  53. surfdemo_show(striped, 'striped')
  54. #rgbarray
  55. imagename = os.path.join(main_dir, 'data', 'arraydemo.bmp')
  56. imgsurface = pygame.image.load(imagename)
  57. rgbarray = surfarray.array3d(imgsurface)
  58. surfdemo_show(rgbarray, 'rgbarray')
  59. #flipped
  60. flipped = rgbarray[:,::-1]
  61. surfdemo_show(flipped, 'flipped')
  62. #scaledown
  63. scaledown = rgbarray[::2,::2]
  64. surfdemo_show(scaledown, 'scaledown')
  65. #scaleup
  66. #the element type is required for N.zeros in NumPy else
  67. #an #array of floats is returned.
  68. shape = rgbarray.shape
  69. scaleup = N.zeros((shape[0]*2, shape[1]*2, shape[2]), int32)
  70. scaleup[::2,::2,:] = rgbarray
  71. scaleup[1::2,::2,:] = rgbarray
  72. scaleup[:,1::2] = scaleup[:,::2]
  73. surfdemo_show(scaleup, 'scaleup')
  74. #redimg
  75. redimg = N.array(rgbarray)
  76. redimg[:,:,1:] = 0
  77. surfdemo_show(redimg, 'redimg')
  78. #soften
  79. #having factor as an array forces integer upgrade during multiplication
  80. #of rgbarray, even for numpy.
  81. factor = N.array((8,), int32)
  82. soften = N.array(rgbarray, int32)
  83. soften[1:,:] += rgbarray[:-1,:] * factor
  84. soften[:-1,:] += rgbarray[1:,:] * factor
  85. soften[:,1:] += rgbarray[:,:-1] * factor
  86. soften[:,:-1] += rgbarray[:,1:] * factor
  87. soften //= 33
  88. surfdemo_show(soften, 'soften')
  89. #crossfade (50%)
  90. src = N.array(rgbarray)
  91. dest = N.zeros(rgbarray.shape) # dest is float64 by default.
  92. dest[:] = 20, 50, 100
  93. diff = (dest - src) * 0.50
  94. xfade = src + diff.astype(uint)
  95. surfdemo_show(xfade, 'xfade')
  96. #alldone
  97. pygame.quit()
  98. if __name__ == '__main__':
  99. main()