0

this is the usual code.

get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH)

pygame.image.save(get_screen, image_filename)  # save the file as jpeg

## reopen image and base64 encode
with open(image_filename, "rb") as imageFile:
    imagestr = base64.b64encode(imageFile.read())
    ## save imagestr(image as a textfle) to disk
    with open(text_filename, "wb") as f:
        f.write(imagestr)

what I would like to do is to convert get_screen into a jpeg image WITHOUT first writing it to disk as in the second line. Some kind of conversion in MEMORY. Is there a way to do that?

wookie
  • 329
  • 1
  • 5
  • 13
  • Convert to PIL and then use http://stackoverflow.com/questions/646286/python-pil-how-to-write-png-image-to-string – Veedrac Sep 09 '14 at 17:40
  • Is `pygame.image.tostring` what you're looking for? http://www.pygame.org/docs/ref/image.html#pygame.image.tostring – robbrit Sep 09 '14 at 17:45
  • I would like to do this WITHOUT using any other module. Let me try image to string. Thank you all. – wookie Sep 09 '14 at 18:02
  • Image.tostring and image.frombuffer both takes too long to convert for any practical purpose so can't use. Takes over 5 seconds to convert a 1280X800 screen. – wookie Sep 09 '14 at 18:37
  • Why excatly do you need an in-memory JPEG? – sloth Sep 10 '14 at 07:14
  • what I would like to do is to convert get_screen into a jpeg image WITHOUT first writing it to disk which is the way pygame works. StringIO and CstringIO too slow. – wookie Sep 11 '14 at 16:56
  • Finally after many experiments I finally settled with the method coded below. – wookie Sep 15 '14 at 06:39

0 Answers0