def test_name(self): type_channel_modes = [('opencv', 'bgr', 'uint8'), ('pil', 'rgb', 'uint8'), ('numpy', 'bgr', 'uint8'), ('opencv', 'bgr', 'float32'), ('numpy', 'bgr', 'float32')] to_np8 = lambda x: imfeat.convert_image(x, {'type': 'numpy', 'dtype': 'uint8', 'mode': 'bgr'}) to_np32 = lambda x: imfeat.convert_image(x, {'type': 'numpy', 'dtype': 'float32', 'mode': 'bgr'}) for fn in ['lena.jpg', 'lena.pgm', 'lena.ppm']: image_np8 = None image_np32 = None for i in load_images(fn): if image_np8 is None: image_np8 = to_np8(i) image_np32 = to_np32(i) np.testing.assert_equal(image_np8, np.array(image_np32 * 255, dtype=np.uint8)) for t, c, m in type_channel_modes: cur_img = imfeat.convert_image(i, {'type': t, 'dtype': m, 'mode': c}) if t == 'opencv': self.assertTrue(isinstance(cur_img, cv.iplimage)) elif t == 'pil': self.assertTrue(Image.isImageType(cur_img)) else: self.assertTrue(isinstance(cur_img, np.ndarray)) if m == 'uint8': np.testing.assert_equal(image_np8, to_np8(cur_img)) else: np.testing.assert_equal(image_np32, to_np32(cur_img))
def FromImage(input_, backend): """Create the initial image layer from some input. :param input_: Input data. If array, values should lie in the range [0, 1]. :type input_: PIL.Image or 2D ndarray of float :returns: Image layer data with values in the range [0, 1]. :rtype: 2D ndarray of float """ if Image.isImageType(input_): input_ = fromimage(input_.convert('L')) input_ = input_.astype(np.float) # Map from [0,255) to [0,1) input_ /= 255 # Note: we could have scaled pixels to [-1,1). This would result in a # doubling of the dynamic range of the S1 response, since each S1 activation # is of the form: # y = XW # where X and W are the input and weight vector, respectively. The rescaled # version of X (call it X') is given by: # X' = 2X - 1 # so the activation is given by # y' = X'W = (2X - 1)W = 2XW - \sum w_i = 2XW # since W is a mean-zero Gabor filter. (This ignores retinal processing, and # nonlinearities caused by normalization). The scaling of S1 response seems # unlikely to cause a significant change in the network output. elif isinstance(input_, np.ndarray): if input_.ndim != 2: raise ValueError("Image array must be 2D") else: raise ValueError("Unknown input value of type: %s" % type(input_)) output = backend.PrepareArray(input_) if np.isnan(output).any(): raise BackendError("Found illegal values in image layer") return output
def MakeState(self, source, copy = False): """Create a model state wrapper for the given image source. :type state: str or Image.Image or 2D array of ACTIVATION_DTYPE or `State` subclass :param state: Source information :param bool copy: If the input is already a state object, this argument determines whether the state is copied. :rtype: `state.State` subclass If `source` is an array, values should lie in the range [0,1). """ if isinstance(source, self.StateClass): if copy: source = copy_mod.copy(source) # shallow copy return source state = self.StateClass() if isinstance(source, basestring): state[self.LayerClass.SOURCE] = InputSource(source) elif Image.isImageType(source): img = PrepareImage(source, self.params) state[self.LayerClass.IMAGE] = layer.FromImage(img, self.backend) elif isinstance(source, np.ndarray): if source.ndim != 2: raise ValueError("Array inputs must be 2D") if source.dtype != ACTIVATION_DTYPE: raise ValueError("Array values must have type: %s" % ACTIVATION_DTYPE) state[self.LayerClass.IMAGE] = source else: raise ValueError("Image source had unknown type: %s" % source) return state
def convert_image(image, modes): """ Args: image: A PIL image or an OpenCV BGR/Gray image (8 bits per channel) modes: List of valid image types Returns: Valid image Raises: ValueError: There was a problem converting the color. """ if isinstance(image, cv.cvmat): image = cv.GetImage(image) if Image.isImageType(image) and image.mode == 'LA': image = image.convert('L') if Image.isImageType(image) and image.mode not in ('L', 'RGB'): image = image.convert('RGB') if Image.isImageType(image) and (image.mode == 'L' or image.mode == 'RGB'): if image.mode not in modes: image = _convert_pil(image, modes[0]) elif isinstance(image, cv.iplimage) and (image.channels == 1 or image.channels == 3) and image.depth == cv.IPL_DEPTH_8U: mode = 'bgr' if image.channels == 3 else 'gray' if ('opencv', mode, cv.IPL_DEPTH_8U) not in modes: image = _convert_cv(image, modes[0]) elif isinstance(image, cv.iplimage) and (image.channels == 1 or image.channels == 3) and image.depth == cv.IPL_DEPTH_32F: mode = 'bgr' if image.channels == 3 else 'gray' if ('opencv', mode, cv.IPL_DEPTH_32F) not in modes: # Convert to 8bit to bgr image = _convert_depth(image, cv.IPL_DEPTH_8U) image = _convert_cv(image, modes[0]) else: if Image.isImageType(image): raise ValueError('Unknown image type PIL Mode[%s]' % image.mode) else: raise ValueError('Unknown image type[%s]' % repr(image)) return image
def fromimage(im, flatten = 0): """Convert Image to numpy array.""" if isinstance(im, np.ndarray): return im # make function idempotent if not Image.isImageType(im): raise TypeError("Input is not a PIL image.") if flatten: im = im.convert('F') elif im.mode == '1': # Add workaround to bug on converting binary image to numpy array. im = im.convert('L') elif im.mode == 'LA': raise ValueError("Greyscale with alpha channel not supported. Convert to" " RGBA first.") return np.array(im)
def fromimage(im, flatten=0): """Convert Image to numpy array.""" if isinstance(im, np.ndarray): return im # make function idempotent if not Image.isImageType(im): raise TypeError("Input is not a PIL image.") if flatten: im = im.convert('F') elif im.mode == '1': # Add workaround to bug on converting binary image to numpy array. im = im.convert('L') elif im.mode == 'LA': raise ValueError( "Greyscale with alpha channel not supported. Convert to" " RGBA first.") return np.array(im)
def PrepareImage(img, params): """Prepare an image for input into the model. :param Image img: Input data. :param params.Params params: Parameters controlling image transformation. :returns: Image layer data with values in the range [0, 1]. :rtype: 2D ndarray of float The image may be scaled and/or cropped, depending on the settings of the `image_resize_method` attribute of the `params` argument. If the value is NONE, the image is returned unchanged. Given a value of SCALE_SHORT_EDGE, SCALE_LONG_EDGE, SCALE_WIDTH, or SCALE_HEIGHT, the given image edge will be rescaled to match `params.image_resize_length` (preserving the aspect ratio). Finally, the `image_resize_method` attribute could be SCALE_AND_CROP. In this case, the image is scaled and cropped to a fixed size of (w, w/rho), where w is `image_resize_length` and rho is `image_resize_aspect_ratio`. This is achieved scaling the short edge (preserving aspect ratio) and then cropping from the long edge. See also :func:`ScaleImage` and :func:`ScaleAndCropImage`. """ if not Image.isImageType(img): raise ValueError("Bad input type: %s" % type(img)) resize_method = params.image_resize_method if resize_method == ResizeMethod.NONE: return img resize_length = params.image_resize_length old_size = np.array(img.size, np.float) # format is (width, height) if resize_method == ResizeMethod.SCALE_SHORT_EDGE: img = ScaleImage(img, old_size / min(old_size) * resize_length) elif resize_method == ResizeMethod.SCALE_LONG_EDGE: img = ScaleImage(img, old_size / max(old_size) * resize_length) elif resize_method == ResizeMethod.SCALE_WIDTH: img = ScaleImage(img, old_size / old_size[0] * resize_length) elif resize_method == ResizeMethod.SCALE_HEIGHT: img = ScaleImage(img, old_size / old_size[1] * resize_length) elif resize_method == ResizeMethod.SCALE_AND_CROP: width = resize_length height = width / float(params.image_resize_aspect_ratio) img = ScaleAndCropImage(img, (width, height)) else: raise ValueError("Unknown resize method: %s" % resize_method) return img
def _generate_thumbnail(self, image_file, output, size = None): try: if Image.isImageType(image_file): image = image_file else: image = Image.open(image_file) if image.mode != 'RGB': image = image.convert('RGB') if size is None: size = [self.Constants.PREVIEW_IMAGE_WIDTH, self.Constants.PREVIEW_IMAGE_HEIGHT] image.thumbnail( size, Image.ANTIALIAS ) image.save(output, 'JPEG') except IOError: logger.exception('Could not generate thumbnail') raise PreviewImageGenerationFailed()
def __init__(self, floor_image, collector): """ [floor_image] will be either a file name or a PIL image. All coordinates for internal structures of this floor will be relative to the image dimensions. Upper left corner is (0,0). [collector] is of class pipe.Collector and serves as the mechanism for getting data [macs] is a list of mac address we are tracking """ self.routers = {} self.centroid_store = defaultdict(lambda : deque(maxlen=5)) self.collector = collector self.r = redis.StrictRedis() if isinstance(floor_image, str): self.floor_image = Image.open(floor_image) self.floor_image_width = self.floor_image.size[0] self.floor_image_height = self.floor_image.size[1] elif Image.isImageType(floor_image): self.floor_image = floor_image else: raise TypeError('floor_image must be PIL image or filename string')
def fromimage(im, flatten=0): """Return a copy of a PIL image as a numpy array. :Parameters: im : PIL image Input image. flatten : bool If true, convert the output to grey-scale. :Returns: img_array : ndarray The different colour bands/channels are stored in the third dimension, such that a grey-image is MxN, an RGB-image MxNx3 and an RGBA-image MxNx4. """ if not Image.isImageType(im): raise TypeError("Input is not a PIL image.") if flatten: im = im.convert('F') return array(im)
def __init__(self, img=None, cam=None): # Regular interface if type(img) is str: self.img = Image.open(img) elif hasattr(img, 'copy'): self.img = img.copy() else: self.img = img # Numpy interface if Image.isImageType(self.img): self.numpy_img = np.asarray(self.img).copy() self.img = Image.fromarray(self.numpy_img) elif type(self.img) == np.ndarray: self.numpy_img = self.img self.img = Image.fromarray(self.numpy_img) else: self.numpy_img = None if cam is not None: self.cam = cam.copy()
def extract_colors(filename_or_img, min_saturation=MIN_SATURATION, min_distance=MIN_DISTANCE, max_colors=MAX_COLORS, min_prominence=MIN_PROMINENCE, n_quantized=N_QUANTIZED): """ Determine what the major colors are in the given image. """ if Im.isImageType(filename_or_img): im = filename_or_img else: im = Im.open(filename_or_img) # get point color count if im.mode != 'RGB': im = im.convert('RGB') im = autocrop(im, WHITE) # assume white box im = im.convert('P', palette=Im.ADAPTIVE, colors=n_quantized, ).convert('RGB') data = im.getdata() dist = Counter(data) n_pixels = mul(*im.size) # aggregate colors to_canonical = {WHITE: WHITE, BLACK: BLACK} aggregated = Counter({WHITE: 0, BLACK: 0}) sorted_cols = sorted(dist.iteritems(), key=itemgetter(1), reverse=True) for c, n in sorted_cols: if c in aggregated: # exact match! aggregated[c] += n else: d, nearest = min((distance(c, alt), alt) for alt in aggregated) if d < min_distance: # nearby match aggregated[nearest] += n to_canonical[c] = nearest else: # no nearby match aggregated[c] = n to_canonical[c] = c # order by prominence colors = sorted((Color(c, n / float(n_pixels)) \ for (c, n) in aggregated.iteritems()), key=attrgetter('prominence'), reverse=True) colors, bg_color = detect_background(im, colors, to_canonical) # keep any color which meets the minimum saturation sat_colors = [c for c in colors if meets_min_saturation(c, min_saturation)] if bg_color and not meets_min_saturation(bg_color, min_saturation): bg_color = None if sat_colors: colors = sat_colors else: # keep at least one color colors = colors[:1] # keep any color within 10% of the majority color colors = [c for c in colors if c.prominence >= colors[0].prominence * min_prominence][:max_colors] return Palette(colors, bg_color)
def toimage(arr, *args, **kw): if Image.isImageType(arr): return arr return scipy.misc.toimage(arr, *args, **kw)