Skip to content

Instantly share code, notes, and snippets.

@harjitmoe
Last active January 19, 2017 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harjitmoe/caf481c0c3a5dea8f94f931afa97ba19 to your computer and use it in GitHub Desktop.
Save harjitmoe/caf481c0c3a5dea8f94f931afa97ba19 to your computer and use it in GitHub Desktop.
Glue code for using PIL to load images into Pygame
from PIL import Image as pil_image
from pygame import image as pygame_image
def pil2pygame(im):
return pygame_image.frombuffer(im.convert("RGBA").tostring(),im.size,"RGBA")
def open_(name):
try:
im=pil_image.open(name)
except IOError,e: #Compatibility with Pygame
from pygame import base
raise base.error(str(e))
return pil2pygame(im)
def _pygame__image__load(*args):
if len(args)==1:
return open_(*args)
else:
return _old_pygame__image__load(*args)
_old_pygame__image__load,pygame_image.load=pygame_image.load,_pygame__image__load
pil_image.init()
exts=sorted(filter(lambda i:pil_image.EXTENSION[i] in pil_image.OPEN.keys(),pil_image.EXTENSION.keys()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment