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

Re: [pygame] image conversion in memory



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