[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Pygame surface



Thank you for the response Peter. Pygame image to string code below

def displayscreen():
  ## image_filename = str(va.page) + ".jpeg" Â# va.page is the current page no
  filename = "zxs" + str(va.page) + ".ada"
  ## images are saved in runs.py and to use it storeno = 2
  image2blit = getfrompy.main(filename, storeno = 2)
  if image2blit == None:
    ## no image saved in file so draw the screen with make_screen_text
    make_screen_text()
    ## now grab the screen
    screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH)
    ## convert to string
    screenstr = pygame.image.tostring(screen, "RGBA", Falsey)
    ## nowencode and write to dict in runs.py
    strA = XOR(screenstr, coder)
    str1 = base64.b64encode(strA)
    scrnstore [filename] = str1
    image2blit = pygame.image.frombuffer(screenstr, ( SCREENW, SCREENH ), 'RGBA')
  va.getscr = True
  return image2blit

this code took too long to convert back and fourth. around 6 seconds.

      screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH)
      image2blit = pygame.Surface.convert_alpha(screen)
      memsave = StringIO()
      pygame.image.save(image2blit, memsave)
      memsave.seek(0)
      pygame.image.load(memsave, iname) Â<- code crashes here
      strA = XOR(iname, coder)
      str1 = base64.b64encode(strA)
      scrnstore [filename] = str1

the above code crashes at the point marked with the message pygame.error: unsupported image format. What I want to do is to grab the screen surface, convert it to a string format and save to a dictionary.
The dictionary will be pickled and saved to a file.
I want all my images in this file and NOT AS IMAGE FILES ON DISK.
Please help to resolve this.





On Wed, Sep 10, 2014 at 10:05 AM, Peter Shinners <pete@xxxxxxxxxxxx> wrote:
On 09/09/2014 04:57 AM, diliup gabadamudalige wrote:

How can a Pygame surface be converted to an image file say a jpeg or a png WITHOUT writing the file to disk? Say after a screen grab?



It is not highly documented, but you can pass a file-like object to pygame.image.save. So instead of a filename you could create a StringIO buffer and write into that. Or you could use something crazy like an html post request and upload directly to a website.

If you are trying to load the image into a different library, it will be easier to use the "tobuffer" or "tostring" methods on the Surface. Then hope the other library you are using has a fromstring, frombuffer, or fromdata style function.





--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions.
**********************************************************************************************