Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Apr 23, 2018

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
You can’t perform that action at this time.