3

I tried to save a pygame.Surface but it doesn't let me, error

TypeError: can't pickle Surface objects

I can make it save surfaces? Or maybe there is another module that can save it ?

EXPLANATION:

a = pygame.Surface( (5,5) )
file = open("hello", "w")
pickle.dump(a, file)

I have classes which saves in them Surfaces.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Ofek .T.
  • 741
  • 3
  • 10
  • 29
  • What do you mean by "save" the surface? Set it to a variable? Please post the code that is giving you this error. – scohe001 Aug 24 '13 at 02:03
  • 3
    You can pickle the filepath or key to a dict of loaded textures. You don't want/need to pickle the actual Surface object. – ninMonkey Aug 24 '13 at 03:02

1 Answers1

5

As monkey said: You don't want to pickle a surface. But if you really need to save that surfaces' content than use the pygame.image.save() function.

If you prefer your surface not to be an actual image file (for whatever reason) you could use the pygame.image.tostring() function which lets you convert the surfaces' content into a stringbuffer.

ato
  • 854
  • 1
  • 6
  • 9