Ejemplo n.º 1
0
    def resize(self, shape, electrodes=None):
        """Resize the image

        Parameters
        ----------
        shape : (rows, cols)
            Shape of the resized image
        electrodes : int, string or list thereof; optional
            Optionally, you can provide your own electrode names. If none are
            given, electrode names will be numbered 0..N.

            .. note::
               The number of electrode names provided must match the number of
               pixels in the grayscale image.

        Returns
        -------
        stim : `ImageStimulus`
            A copy of the stimulus object containing the resized image

        """
        height, width = shape
        if height < 0 and width < 0:
            raise ValueError('"height" and "width" cannot both be -1.')
        if height < 0:
            height = int(self.img_shape[0] * width / self.img_shape[1])
        if width < 0:
            width = int(self.img_shape[1] * height / self.img_shape[0])
        img = img_resize(self.data.reshape(self.img_shape), (height, width))

        return ImageStimulus(img,
                             electrodes=electrodes,
                             metadata=self.metadata)
Ejemplo n.º 2
0
def _render(display, screen_img):
    img2draw = img_resize(image=np.flipud(np.rot90(screen_img)),
                          output_shape=(500, 500),
                          preserve_range=True)

    main_surface = pygame.surfarray.make_surface(img2draw)
    display.fill(color="black")
    display.blit(main_surface, [0, 0])
    pygame.display.update()
Ejemplo n.º 3
0
 def __init__(self,
              source,
              format=None,
              resize=None,
              as_gray=False,
              electrodes=None,
              metadata=None,
              compress=False):
     if metadata is None:
         metadata = {}
     elif type(metadata) != dict:
         metadata = {'user': metadata}
     if isinstance(source, str):
         # Filename provided:
         img = imread(source, format=format)
         metadata['source'] = source
         metadata['source_shape'] = img.shape
     elif isinstance(source, ImageStimulus):
         img = source.data.reshape(source.img_shape)
         metadata.update(source.metadata)
         if electrodes is None:
             electrodes = source.electrodes
     elif isinstance(source, np.ndarray):
         img = source
     else:
         raise TypeError("Source must be a filename or another "
                         "ImageStimulus, not %s." % type(source))
     if img.ndim < 2 or img.ndim > 3:
         raise ValueError("Images must have 2 or 3 dimensions, not "
                          "%d." % img.ndim)
     # Convert to grayscale if necessary:
     if as_gray:
         if img.ndim == 3 and img.shape[2] == 4:
             # Blend the background with black:
             img = rgba2rgb(img, background=(0, 0, 0))
         if img.ndim == 3:
             img = rgb2gray(img)
     # Resize if necessary:
     if resize is not None:
         height, width = resize
         if height < 0 and width < 0:
             raise ValueError('"height" and "width" cannot both be -1.')
         if height < 0:
             height = int(img.shape[0] * width / img.shape[1])
         if width < 0:
             width = int(img.shape[1] * height / img.shape[0])
         img = img_resize(img, (height, width))
     # Store the original image shape for resizing and color conversion:
     self.img_shape = img.shape
     # Convert to float array in [0, 1] and call the Stimulus constructor:
     super(ImageStimulus, self).__init__(img_as_float32(img).ravel(),
                                         time=None,
                                         electrodes=electrodes,
                                         metadata=metadata,
                                         compress=compress)
     self.metadata = metadata
Ejemplo n.º 4
0
 def resize(self, shape, electrodes=None):
     img = img_resize(self.data.reshape(self.img_shape), shape)
     if electrodes is None:
         electrodes = np.arange(img.size)
     self._stim = {
         'data': img.reshape((-1, 1)),
         'electrodes': electrodes,
         'time': None
     }
     self.img_shape = img.shape
     return self
Ejemplo n.º 5
0
 def __init__(self, fname, format=None, resize=None, as_gray=False,
              electrodes=None, metadata=None, compress=False):
     img = imread(fname, format=format)
     # Build the metadata container:
     if metadata is None:
         metadata = {}
     metadata['source'] = fname
     metadata['source_shape'] = img.shape
     # Convert to grayscale if necessary:
     if as_gray:
         if img.shape[-1] == 4:
             # Convert the transparent background to black:
             img = rgba2rgb(img, background=(0, 0, 0))
         img = rgb2gray(img)
     # Resize if necessary:
     if resize is not None:
         img = img_resize(img, resize)
     # Store the original image shape for resizing and color conversion:
     self.img_shape = img.shape
     # Convert to float array in [0, 1] and call the Stimulus constructor:
     super(ImageStimulus, self).__init__(img_as_float(img).ravel(),
                                         time=None, electrodes=electrodes,
                                         metadata=metadata,
                                         compress=compress)