Saving PNG from PyGame

The latest version, 1.8, of PyGame can save PNGs directly from a Surface: pygame.Image.save(mySurface, 'myimagefile.png'). But what if you want to support an older version of PyGame, such as the one available for the N800 or the XO? Well, assuming you have access to the Python Image Library, you can use that:

import Image # from PIL
import pygame

def pygame_to_pil_img(pg_surface):
    imgstr = pygame.image.tostring(pg_surface, 'RGB')
    return Image.fromstring('RGB', pg_surface.get_size(), imgstr)

def pil_to_pygame_img(pil_img):
    imgstr = pil_img.tostring()
    return pygame.image.fromstring(imgstr, pil_img.size, 'RGB')

Once you have a PyGame Image, you can save it to PNG easily: myImage.save('myfilename.png')

I’ve found myself looking for this code snippet more than once, now I can Google for it more readily, and maybe someone else will find it helpful too.

7 Comments

  1. Monday: saving images from Python … « Jon’s PhD Journal said,

    November 24, 2008 at 12:05 pm

    [...] Monday: saving images from Python … Filed under: Notes — JDE @ 7:53 pm http://livingcode.org/2008/saving-png-from-pygame#respond [...]

  2. atm0s1 said,

    December 11, 2008 at 7:03 am

    hi,

    I’m trying to create an image in PIL, what I want to do is draw two polygons that overlap … how do you set a shapes fill opacity ? The effect that I’m trying to achieve
    is like http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/

  3. delza said,

    December 11, 2008 at 9:12 am

    Hi Roger, I enjoyed your post on generating the Mona Lisa using polygons. Looking at the PIL documentation, I don’t see any way to draw polygons using opacity. There is an optional PIL library, aggdraw, which supports opacity (alpha):

    http://effbot.org/zone/pythondoc-aggdraw.htm

    I will try to make a follow-up post demonstrating some ways to draw polygons with opacity in Python shortly.

  4. atm0s1 said,

    December 11, 2008 at 11:10 am

    figured it out thanks to your aggdraw tip

    from PIL import Image,ImageDraw
    import aggdraw
    img=Image.new(’RGBA’,(200,200),(255,255,255))
    d = aggdraw.Draw(img)
    p = aggdraw.Pen(”red”, 0.5,50)
    b = aggdraw.Brush(”red”)
    d.polygon([60,160,90,60,190,190,60,190],p,b)
    b = aggdraw.Brush(”yellow”,50)
    d.polygon([0,0,90,60,90,190,0,90],b)
    d.line((0, 0, 500, 500), p)
    d.flush()
    img.save(’test.png’,'PNG’)

  5. delza said,

    December 11, 2008 at 11:17 am

    Cool! I’m looking forward to seeing how you wrote the genetic algorithm for Mona Lisa. Glad if I could help in some small way.

  6. atm0s1 said,

    December 11, 2008 at 11:34 am

    i’ll give it a shot and post back the results ;-)

  7. Living Code » Blog Archive » Drawing with opacity said,

    January 3, 2009 at 11:54 am

    [...] is something of a followup to earlier posts Drawing Hexmaps and Saving PNG from PyGame. A recent comment from Roger Aisling on the second of those posts asked about drawing with opacity [...]

Post a Comment

You must bee logged in to post a comment.

google

google

asus