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

Re: [pygame] image conversion in memory



Hi Christopher!

Thanks for the response. Below is my code

      pygame.image.save(get_screen, image_filename) Â# save the file as jpeg
      ## reopen image and convert to text file
      with open(image_filename, "rb") as imageFile:
        imagestr = base64.b64encode(imageFile.read())
      ## create the text file name to save to disk
      text_filename = newpath + "s" + str(va.page) + ".dat"
      ## save imagestr(image as a textfle) to disk
      with open(text_filename, "wb") as f:
        f.write(imagestr)

      myscreen = pygame.image.load(image_filename).convert()

I capture the screen image
Is there any way that it can be converted directly to a text file like above without writing it to disk first?
When you capture a screen it is a pygame surface. It is NOT a jpeg or a png. The only way to cojnvert it to a jpeg or png is to write it to disk and then reload it and then convert it to a text fle using base64.

What I would like to do is to capture the screen, covert to jpeg and then convert to text file WITHOUT writing to disk first.

Any ideas?
Thanks for your support.




On Fri, Aug 8, 2014 at 7:54 PM, Christopher Night <cosmologicon@xxxxxxxxx> wrote:
It depends on what precisely you mean when you say that a python variable is a jpeg image. Do you mean that it's a string containing the contents of a jpeg image file? So if you wrote it to disk you'd get a jpeg image file?

If that's what you mean, you can do it with PIL and StringIO. (There might be an easier way.)

import pygame
from PIL import Image
from cStringIO import StringIO

# create an example pygame image
img = pygame.Surface((100, 100))
pygame.draw.circle(img, (255, 0, 0), (50, 50), 40)
# convert to PIL format
imgstr = pygame.image.tostring(img, "RGB", False)
pimg = Image.fromstring("RGB", img.get_size(), imgstr)
# save to string
s = StringIO()
pimg.save(s, "JPEG")
r = s.getvalue()
s.close()

# r is a string with the contents of a jpeg file. to confirm:
open("img.jpg", "wb").write(r)


However, this doesn't seem like a very useful thing to have (and if I needed it so badly I would just write it to disk and read it back) so I may be misunderstanding you.


On Fri, Aug 8, 2014 at 5:16 AM, diliup gabadamudalige <diliupg@xxxxxxxxx> wrote:
Hi all!

Is there a way to convert a pygame surface to a png or jpeg IN MEMORY instead of saving to disk and loading again?
something like
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen

my_image = get_screen.convert(jpeg) # now convert the image to jpeg

and now my_image is a jpeg image of get_screen

I searched the net but couldn't find any other way other than save to disk as jpeg and reload.

Any positive OR negative help is very much appreciated.

Thanks in advance
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.
**********************************************************************************************





--
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.
**********************************************************************************************