Last active
January 19, 2017 17:44
-
-
Save harjitmoe/caf481c0c3a5dea8f94f931afa97ba19 to your computer and use it in GitHub Desktop.
Glue code for using PIL to load images into Pygame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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