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

Fwd: Re: [pygame] image conversion in memory



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