Skip to content

Instantly share code, notes, and snippets.

@lindemann09
Last active April 23, 2018 12:04
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 lindemann09/3d9f114081618dfba99a3ebfa9a7663b to your computer and use it in GitHub Desktop.
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)
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)
@lindemann09
Copy link
Author

usage

from expyriment_pil_image import ExprimentPILImage

.....
stim = ExprimentPILImage(my_pillow_image, position=(0, 0)
...
stim.present()
....

....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment