Last active
April 23, 2018 12:04
-
-
Save lindemann09/3d9f114081618dfba99a3ebfa9a7663b to your computer and use it in GitHub Desktop.
Use PIL (Pillow) Images in Expyriment (will be soon avaiable via the stash)
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 __future__ import absolute_import, print_function, division | |
from builtins import * | |
__author__ = 'Oliver Lindemann <oliver.lindemann@cognitive-psychology.eu>' | |
import pygame | |
from PIL.Image import isImageType | |
from expyriment.stimuli import Canvas | |
class ExprimentPILImage(Canvas): | |
def __init__(self, pil_image, position=(0,0)): | |
""""Display PIL-images in Expyriment | |
Usage | |
----- | |
If you have PIL.Image (e.g. `my_pillow_image`) you can depict it as follows: | |
stim = ExprimentPILImage(my_pillow_image, position=(0, 0)) | |
stim.present() | |
"""" | |
if not isImageType(pil_image): | |
raise RuntimeError("pil_image (type: {}) is not a PIL image".format(type(pil_image))) | |
Canvas.__init__(self, size=pil_image.size, position=position) | |
self._image = pil_image | |
def _create_surface(self): | |
return pygame.image.fromstring(self._image.tobytes(), | |
self.size, | |
self._image.mode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage
....