def encode_PIL_Image(self, image): # convert to numpy array image = np.array(image) # add channel axis if input is grayscale image if image.ndim == 2: image = image[..., np.newaxis] elif image.ndim != 3: raise ValueError("Unhandled number of channels: %d" % image.ndim) # transpose to CHW image = image.transpose(2, 0, 1) return image
def encode_PIL_Image(self, image): if self.channel_conversion != 'none': if image.mode != self.channel_conversion: # convert to different image mode if necessary image = image.convert(self.channel_conversion) # convert to numpy array image = np.array(image) # add channel axis if input is grayscale image if image.ndim == 2: image = image[..., np.newaxis] elif image.ndim != 3: raise ValueError("Unhandled number of channels: %d" % image.ndim) # transpose to CHW image = image.transpose(2, 0, 1) return image
def encode_PIL_Image(self, image, channel_conversion='none'): if channel_conversion != 'none': if image.mode != channel_conversion: # convert to different image mode if necessary image = image.convert(channel_conversion) # convert to numpy array image = np.array(image) # add channel axis if input is grayscale image if image.ndim == 2: image = image[..., np.newaxis] elif image.ndim != 3: raise ValueError("Unhandled number of channels: %d" % image.ndim) # transpose to CHW image = image.transpose(2, 0, 1) return image