Search code examples
pythonimagepygamegeometry-surface

Pygame - Store surface as image


This is just my second day at pygame, and I am not much aware about many functions. The following is a sample of what I am doing on my surface(window)

for c in nStations:
  pygame.time.wait( 20 )
  pygame.draw.circle( window, c.tColor, c.tPosition, c.iRadius )
  pygame.display.update()

This is looped for n number of circles(in a tuple) to be displayed. Now, this will be repeated a total of 5-6 times. I wish to store/save the surface window as an image(or sub-surface) and put it as a thumbnail/link at the top-right-corner of my window.

Is it possible? Or is pygame not a good library for this? I tried working on cocos2d too, but it's documentation is very poor as compared to pygame.

In case my question didn't make any-sense anywhere, please reply. I'll keep updating it.

P.S. I've seen python.surface library functions. and surface.copy seems to be what I am looking for, but can I store the surface as a thumbnail/image before copying and editing it further?

EDIT->After reading jsbueno's reply, what I did:

  tThumbs.append( pygame.transform.smoothscale( window, (32, 32) ) )
  # the above statement inside drawing-circle loop

i = 1
for thumb in tThumbs:
  window.blit( thumb, (1050, 36*i) )
  pygame.display.update()
  i += 1
  pygame.time.wait( 200 )

The above seems to work perfectly for me. Thanks. :)


Solution

  • Indeed - all drawing and transofmr functions in Pygame accept a "surface" as the first parameter. Actually, in pygame, a "surface" is an image.

    The display window is just a surface as well - although a specialized one, whose contents are reflected on screen. But you can use pygame.transform.smoothscale to get a thumbnail copy of your screen surface, and then, the blit method of your screen object itself to paste it on screen.

    If this is going to be done continuoulsy, the pasted corner ould also be replicated, like a video camera pointing to its own output - so the "right way" to do it is: keep yoru drawing ina separate, in memory surface, blit it to the screen surface, and then blit the thumbnail above it.