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

Re: Re: [pygame] image conversion in memory



get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen

Âhow can you concert get_screen to a jpeg or a png WITHOUT saving to disk? In Pygame..

Thanks


On Mon, Aug 11, 2014 at 12:21 AM, Radomir Dopieralski <pygame@xxxxxxxxxxxx> wrote:
On 08/08/2014 11:16 AM, diliup gabadamudalige 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.

Sure, just use StringIO to use memory instead of a file.

import pygame
import StringIO

s = pygame.image.load("yourimage.png")
f = StringIO.StringIO()
f.name = 'yourimage.jpg'
pygame.image.save(s, f)

yourimage = f.getvalue()


--
Radomir Dopieralski





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