""" The image module supports basic image loading, rescaling and display operations. """
# For clarity, names from _image are given explicitly in this module:
# For user convenience, the names from _image are also imported into # the image namespace:
IdentityTransform, TransformedBbox)
# map interpolation strings to module constants 'none': _image.NEAREST, # fall back to nearest when not supported 'nearest': _image.NEAREST, 'bilinear': _image.BILINEAR, 'bicubic': _image.BICUBIC, 'spline16': _image.SPLINE16, 'spline36': _image.SPLINE36, 'hanning': _image.HANNING, 'hamming': _image.HAMMING, 'hermite': _image.HERMITE, 'kaiser': _image.KAISER, 'quadric': _image.QUADRIC, 'catrom': _image.CATROM, 'gaussian': _image.GAUSSIAN, 'bessel': _image.BESSEL, 'mitchell': _image.MITCHELL, 'sinc': _image.SINC, 'lanczos': _image.LANCZOS, 'blackman': _image.BLACKMAN, }
""" Composite a number of RGBA images into one. The images are composited in the order in which they appear in the `images` list.
Parameters ---------- images : list of Images Each must have a `make_image` method. For each image, `can_composite` should return `True`, though this is not enforced by this function. Each image must have a purely affine transformation with no shear.
renderer : RendererBase instance
magnification : float The additional magnification to apply for the renderer in use.
Returns ------- tuple : image, offset_x, offset_y Returns the tuple:
- image: A numpy array of the same type as the input images.
- offset_x, offset_y: The offset of the image (left, bottom) in the output figure. """ if len(images) == 0: return np.empty((0, 0, 4), dtype=np.uint8), 0, 0
parts = [] bboxes = [] for image in images: data, x, y, trans = image.make_image(renderer, magnification) if data is not None: x *= magnification y *= magnification parts.append((data, x, y, image.get_alpha() or 1.0)) bboxes.append( Bbox([[x, y], [x + data.shape[1], y + data.shape[0]]]))
if len(parts) == 0: return np.empty((0, 0, 4), dtype=np.uint8), 0, 0
bbox = Bbox.union(bboxes)
output = np.zeros( (int(bbox.height), int(bbox.width), 4), dtype=np.uint8)
for data, x, y, alpha in parts: trans = Affine2D().translate(x - bbox.x0, y - bbox.y0) _image.resample(data, output, trans, _image.NEAREST, resample=False, alpha=alpha)
return output, bbox.x0 / magnification, bbox.y0 / magnification
renderer, parent, artists, suppress_composite=None): """ Draw a sorted list of artists, compositing images into a single image where possible.
For internal matplotlib use only: It is here to reduce duplication between `Figure.draw` and `Axes.draw`, but otherwise should not be generally useful. """
# override the renderer default if suppressComposite is not None else renderer.option_image_nocomposite())
else: # Composite any adjacent images together image_group = [] mag = renderer.get_image_magnification()
def flush_images(): if len(image_group) == 1: image_group[0].draw(renderer) elif len(image_group) > 1: data, l, b = composite_images(image_group, renderer, mag) if data.size != 0: gc = renderer.new_gc() gc.set_clip_rectangle(parent.bbox) gc.set_clip_path(parent.get_clip_path()) renderer.draw_image(gc, np.round(l), np.round(b), data) gc.restore() del image_group[:]
for a in artists: if isinstance(a, _ImageBase) and a.can_composite(): image_group.append(a) else: flush_images() a.draw(renderer) flush_images()
""" Convert an RGB image to RGBA, as required by the image resample C++ extension. """ rgba = np.zeros((A.shape[0], A.shape[1], 4), dtype=A.dtype) rgba[:, :, :3] = A if rgba.dtype == np.uint8: rgba[:, :, 3] = 255 else: rgba[:, :, 3] = 1.0 return rgba
def __str__(self): return "AxesImage(%g,%g;%gx%g)" % tuple(self.axes.bbox.bounds)
cmap=None, norm=None, interpolation=None, origin=None, filternorm=True, filterrad=4.0, resample=False, **kwargs ): """ interpolation and cmap default to their rc settings
cmap is a colors.Colormap instance norm is a colors.Normalize instance to map luminance to 0-1
extent is data axes (left, right, bottom, top) for making image plots registered with data plots. Default is to label the pixel centers with the zero-based row and column indices.
Additional kwargs are matplotlib.artist properties
"""
state = super().__getstate__() # We can't pickle the C Image cached object. state['_imcache'] = None return state
"""Get the numrows, numcols of the input image""" if self._A is None: raise RuntimeError('You must first set the image array')
return self._A.shape[:2]
""" Set the alpha value used for blending - not supported on all backends.
Parameters ---------- alpha : float """
""" Call this whenever the mappable is changed so observers can update state """
unsampled=False, round_to_pixel_border=True): """ Normalize, rescale and color the image `A` from the given in_bbox (in data space), to the given out_bbox (in pixel space) clipped to the given clip_bbox (also in pixel space), and magnified by the magnification factor.
`A` may be a greyscale image (MxN) with a dtype of `float32`, `float64`, `float128`, `uint16` or `uint8`, or an RGBA image (MxNx4) with a dtype of `float32`, `float64`, `float128`, or `uint8`.
If `unsampled` is True, the image will not be scaled, but an appropriate affine transformation will be returned instead.
If `round_to_pixel_border` is True, the output image size will be rounded to the nearest pixel boundary. This makes the images align correctly with the axes. It should not be used in cases where you want exact scaling, however, such as FigureImage.
Returns the resulting (image, x, y, trans), where (x, y) is the upper left corner of the result in pixel space, and `trans` is the affine transformation from the image to pixel space. """ raise RuntimeError('You must first set the image ' 'array or the image attribute') raise RuntimeError("_make_image must get a non-empty image. " "Your Artist's draw method must filter before " "this method is called.")
return None, 0, 0, None
return None, 0, 0, None
# Flip the input image using a transform. This avoids the # problem with flipping the array, which results in a copy # when it is converted to contiguous in the C wrapper else:
Affine2D() .scale( in_bbox.width / A.shape[1], in_bbox.height / A.shape[0]) .translate(in_bbox.x0, in_bbox.y0) + self.get_transform())
+ Affine2D().translate( -clipped_bbox.x0, -clipped_bbox.y0) .scale(magnification, magnification))
# So that the image is aligned with the edge of the axes, we want # to round up the output width to the next integer. This also # means scaling the transform just slightly to account for the # extra subpixel. (out_width_base % 1.0 != 0.0 or out_height_base % 1.0 != 0.0)): else: out_width = int(out_width_base) out_height = int(out_height_base)
raise ValueError("Invalid dimensions, got {}".format(A.shape))
# if we are a 2D array, then we are running through the # norm + colormap transformation. However, in general the # input data is not going to match the size on the screen so we # have to resample to the correct number of pixels # need to
# TODO slice input array first # figure out the type we should scale to. For floats, # leave as is. For integers cast to an appropriate-sized # float. Small integers get smaller floats in an attempt # to keep the memory footprint reasonable. # all masked, so values don't matter a_min, a_max = np.int32(0), np.int32(1) # Cast to float64 warnings.warn( "Casting input data from '{0}' to 'float64'" "for imshow".format(A.dtype)) else: # probably an integer of some type. da = a_max.astype(np.float64) - a_min.astype(np.float64) if da > 1e8: # give more breathing room if a big dynamic range scaled_dtype = np.float64 else: scaled_dtype = np.float32
# scale the input data to [.1, .9]. The Agg # interpolators clip to [0, 1] internally, use a # smaller input scale to identify which of the # interpolated points need to be should be flagged as # over / under. # This may introduce numeric instabilities in very broadly # scaled data # clip scaled data around norm if necessary. # This is necessary for big numbers at the edge of # float64's ability to represent changes. Applying # a norm first would be good, but ruins the interpolation # of over numbers. np.float64(self.norm.vmin)) else: a_min = np.float64(newmin) else: a_max = np.float64(newmax) A_scaled = np.clip(A_scaled, newmin, newmax)
# a_min and a_max might be ndarray subclasses so use # item to avoid errors
dtype=A_scaled.dtype) # resample the input data to the correct resolution and shape t, _interpd_[self.get_interpolation()], self.get_resample(), 1.0, self.get_filternorm(), self.get_filterrad())
# we are done with A_scaled now, remove from namespace # to be sure! # un-scale the resampled data to approximately the # original range things that interpolated to above / # below the original min/max will still be above / # below, but possibly clipped in the case of higher order # interpolation + drastically changing data. # if using NoNorm, cast back to the original datatype A_resampled = A_resampled.astype(A.dtype)
# this is the case of a nontrivial mask np.float32(1)) else: mask[:] = 1
# we always have to interpolate the mask to account for # non-affine transformations dtype=mask.dtype) t, _interpd_[self.get_interpolation()], True, 1, self.get_filternorm(), self.get_filterrad()) # we are done with the mask, delete from namespace to be sure! # Agg updates the out_mask in place. If the pixel has # no image data it will not be updated (and still be 0 # as we initialized it), if input data that would go # into that output pixel than it will be `nan`, if all # the input data for a pixel is good it will be 1, and # if there is _some_ good data in that output pixel it # will be between [0, 1] (such as a rotated image).
# mask and run through the norm else: # Always convert to RGBA, even if only RGB input A = _rgb_to_rgba(A) raise ValueError("Invalid dimensions, got %s" % (A.shape,))
A, output, t, _interpd_[self.get_interpolation()], self.get_resample(), alpha, self.get_filternorm(), self.get_filterrad())
# at this point output is either a 2D array of normed data # (of int or float) # or an RGBA array of re-sampled input # output is now a correctly sized RGBA array of uint8
# Apply alpha *after* if the input was greyscale without a mask alpha = 1 np.asarray(alpha_channel, np.float32) * out_alpha * alpha, np.uint8)
else: if self._imcache is None: self._imcache = self.to_rgba(A, bytes=True, norm=(A.ndim == 2)) output = self._imcache
# Subset the input image to only the part that will be # displayed subset = TransformedBbox( clip_bbox, t0.frozen().inverted()).frozen() output = output[ int(max(subset.ymin, 0)): int(min(subset.ymax + 1, output.shape[0])), int(max(subset.xmin, 0)): int(min(subset.xmax + 1, output.shape[1]))]
t = Affine2D().translate( int(max(subset.xmin, 0)), int(max(subset.ymin, 0))) + t
raise RuntimeError('The make_image method must be overridden.')
""" draw unsampled image. The renderer should support a draw_image method with scale parameter. """
im, l, b, trans = self.make_image(renderer, unsampled=True)
if im is None: return
trans = Affine2D().scale(im.shape[1], im.shape[0]) + trans
renderer.draw_image(gc, l, b, im, trans)
""" return True if the image is better to be drawn unsampled. The derived class needs to override it. """ return False
def draw(self, renderer, *args, **kwargs): # if not visible, declare victory and return self.stale = False return
# for empty images, there is nothing to draw! self.stale = False return
# actually render the image.
self.get_transform().is_affine): self._draw_unsampled_image(renderer, gc) else: renderer, renderer.get_image_magnification())
""" Test whether the mouse event occurred within the image. """ if callable(self._contains): return self._contains(self, mouseevent) # TODO: make sure this is consistent with patch and patch # collection on nonlinear transformed coordinates. # TODO: consider returning image coordinates (shouldn't # be too difficult given that the image is rectilinear x, y = mouseevent.xdata, mouseevent.ydata xmin, xmax, ymin, ymax = self.get_extent() if xmin > xmax: xmin, xmax = xmax, xmin if ymin > ymax: ymin, ymax = ymax, ymin
if x is not None and y is not None: inside = (xmin <= x <= xmax) and (ymin <= y <= ymax) else: inside = False
return inside, {}
"""Write the image to png file with fname""" im = self.to_rgba(self._A[::-1] if self.origin == 'lower' else self._A, bytes=True, norm=True) _png.write_png(im, fname)
""" Set the image array.
Note that this function does *not* update the normalization used.
Parameters ---------- A : array-like """ # check if data is PIL Image without importing Image if A.mode == 'L': # greyscale image, but our logic assumes rgba: self._A = pil_to_array(A.convert('RGBA')) else: self._A = pil_to_array(A) else:
not np.can_cast(self._A.dtype, float, "same_kind")): raise TypeError("Image data cannot be converted to float")
or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]): raise TypeError("Invalid dimensions for image data")
# If the input data has values outside the valid range (after # normalisation), we issue a warning and then clip X to the bounds # - otherwise casting wraps extreme values, hiding outliers and # making reliable interpretation impossible. _log.warning( 'Clipping input data to the valid range for imshow with ' 'RGB data ([0..1] for floats or [0..255] for integers).' ) self._A = np.clip(self._A, 0, high) # Cast unsupported integer types to uint8 np.integer): self._A = self._A.astype(np.uint8)
""" Retained for backwards compatibility - use set_data instead.
Parameters ---------- A : array-like """ # This also needs to be here to override the inherited # cm.ScalarMappable.set_array method so it is not invoked by mistake.
self.set_data(A)
""" Return the interpolation method the image uses when resizing.
One of 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos', or 'none'.
"""
""" Set the interpolation method the image uses when resizing.
if None, use a value from rc setting. If 'none', the image is shown as is without interpolating. 'none' is only supported in agg, ps and pdf backends and will fall back to 'nearest' mode for other backends.
Parameters ---------- s : {'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', \ 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian', \ 'bessel', 'mitchell', 'sinc', 'lanczos', 'none'}
""" raise ValueError('Illegal interpolation string')
""" Returns `True` if the image can be composited with its neighbors. """ trans = self.get_transform() return ( self._interpolation != 'none' and trans.is_affine and trans.is_separable)
""" Set whether or not image resampling is used.
Parameters ---------- v : bool """
"""Return the image resample boolean."""
""" Set whether the resize filter normalizes the weights.
See help for `~.Axes.imshow`.
Parameters ---------- filternorm : bool """
"""Return whether the resize filter normalizes the weights."""
""" Set the resize filter radius only applicable to some interpolation schemes -- see help for imshow
Parameters ---------- filterrad : positive float """ raise ValueError("The filter radius must be a positive number")
"""Return the filterrad setting."""
def __str__(self): return "AxesImage(%g,%g;%gx%g)" % tuple(self.axes.bbox.bounds)
cmap=None, norm=None, interpolation=None, origin=None, extent=None, filternorm=1, filterrad=4.0, resample=False, **kwargs ):
""" interpolation and cmap default to their rc settings
cmap is a colors.Colormap instance norm is a colors.Normalize instance to map luminance to 0-1
extent is data axes (left, right, bottom, top) for making image plots registered with data plots. Default is to label the pixel centers with the zero-based row and column indices.
Additional kwargs are matplotlib.artist properties
"""
ax, cmap=cmap, norm=norm, interpolation=interpolation, origin=origin, filternorm=filternorm, filterrad=filterrad, resample=resample, **kwargs )
x0, x1, y0, y1 = self._extent bbox = Bbox.from_extents([x0, y0, x1, y1]) return bbox.transformed(self.axes.transData)
# image is created in the canvas coordinate.
self._A, bbox, transformed_bbox, self.axes.bbox, magnification, unsampled=unsampled)
""" Return whether the image would be better drawn unsampled. """ and renderer.option_scale_image())
""" extent is data axes (left, right, bottom, top) for making image plots
This updates ax.dataLim, and, if autoscaling, sets viewLim to tightly fit the image, regardless of dataLim. Autoscaling state is not changed, so following this with ax.autoscale_view will redo the autoscaling in accord with dataLim. """
"""Get the image extent: left, right, bottom, top""" else: sz = self.get_size() numrows, numcols = sz if self.origin == 'upper': return (-0.5, numcols-0.5, numrows-0.5, -0.5) else: return (-0.5, numcols-0.5, -0.5, numrows-0.5)
"""Get the cursor data for a given event""" xmin, xmax, ymin, ymax = self.get_extent() if self.origin == 'upper': ymin, ymax = ymax, ymin arr = self.get_array() data_extent = Bbox([[ymin, xmin], [ymax, xmax]]) array_extent = Bbox([[0, 0], arr.shape[:2]]) trans = BboxTransform(boxin=data_extent, boxout=array_extent) y, x = event.ydata, event.xdata point = trans.transform_point([y, x]) if any(np.isnan(point)): return None i, j = point.astype(int) # Clip the coordinates at array bounds if not (0 <= i < arr.shape[0]) or not (0 <= j < arr.shape[1]): return None else: return arr[i, j]
""" kwargs are identical to those for AxesImage, except that 'nearest' and 'bilinear' are the only supported 'interpolation' options. """ super().__init__(ax, **kwargs) self.set_interpolation(interpolation)
""" return False. Do not use unsampled image. """ return False
if self._A is None: raise RuntimeError('You must first set the image array')
if unsampled: raise ValueError('unsampled not supported on NonUniformImage')
A = self._A if A.ndim == 2: if A.dtype != np.uint8: A = self.to_rgba(A, bytes=True) self.is_grayscale = self.cmap.is_gray() else: A = np.repeat(A[:, :, np.newaxis], 4, 2) A[:, :, 3] = 255 self.is_grayscale = True else: if A.dtype != np.uint8: A = (255*A).astype(np.uint8) if A.shape[2] == 3: B = np.zeros(tuple([*A.shape[0:2], 4]), np.uint8) B[:, :, 0:3] = A B[:, :, 3] = 255 A = B self.is_grayscale = False
x0, y0, v_width, v_height = self.axes.viewLim.bounds l, b, r, t = self.axes.bbox.extents width = (np.round(r) + 0.5) - (np.round(l) - 0.5) height = (np.round(t) + 0.5) - (np.round(b) - 0.5) width *= magnification height *= magnification im = _image.pcolor(self._Ax, self._Ay, A, int(height), int(width), (x0, x0+v_width, y0, y0+v_height), _interpd_[self._interpolation])
return im, l, b, IdentityTransform()
""" Set the grid for the pixel centers, and the pixel values.
*x* and *y* are monotonic 1-D ndarrays of lengths N and M, respectively, specifying pixel centers
*A* is an (M,N) ndarray or masked array of values to be colormapped, or a (M,N,3) RGB array, or a (M,N,4) RGBA array. """ x = np.array(x, np.float32) y = np.array(y, np.float32) A = cbook.safe_masked_invalid(A, copy=True) if not (x.ndim == y.ndim == 1 and A.shape[0:2] == y.shape + x.shape): raise TypeError("Axes don't match array shape") if A.ndim not in [2, 3]: raise TypeError("Can only plot 2D or 3D data") if A.ndim == 3 and A.shape[2] not in [1, 3, 4]: raise TypeError("3D arrays must have three (RGB) " "or four (RGBA) color components") if A.ndim == 3 and A.shape[2] == 1: A.shape = A.shape[0:2] self._A = A self._Ax = x self._Ay = y self._imcache = None
self.stale = True
raise NotImplementedError('Method not supported')
""" Parameters ---------- s : str, None Either 'nearest', 'bilinear', or ``None``. """ if s is not None and s not in ('nearest', 'bilinear'): raise NotImplementedError('Only nearest neighbor and ' 'bilinear interpolations are supported') AxesImage.set_interpolation(self, s)
if self._A is None: raise RuntimeError('Must set data first') return self._Ax[0], self._Ax[-1], self._Ay[0], self._Ay[-1]
pass
pass
if self._A is not None: raise RuntimeError('Cannot change colors after loading data') super().set_norm(norm)
if self._A is not None: raise RuntimeError('Cannot change colors after loading data') super().set_cmap(cmap)
""" Make a pcolor-style plot with an irregular rectangular grid.
This uses a variation of the original irregular image code, and it is used by pcolorfast for the corresponding grid type. """ x=None, y=None, A=None, cmap=None, norm=None, **kwargs ): """ cmap defaults to its rc setting
cmap is a colors.Colormap instance norm is a colors.Normalize instance to map luminance to 0-1
Additional kwargs are matplotlib.artist properties """ super().__init__(ax, norm=norm, cmap=cmap) self.update(kwargs) if A is not None: self.set_data(x, y, A)
if self._A is None: raise RuntimeError('You must first set the image array') if unsampled: raise ValueError('unsampled not supported on PColorImage') fc = self.axes.patch.get_facecolor() bg = mcolors.to_rgba(fc, 0) bg = (np.array(bg)*255).astype(np.uint8) l, b, r, t = self.axes.bbox.extents width = (np.round(r) + 0.5) - (np.round(l) - 0.5) height = (np.round(t) + 0.5) - (np.round(b) - 0.5) # The extra cast-to-int is only needed for python2 width = int(np.round(width * magnification)) height = int(np.round(height * magnification)) if self._rgbacache is None: A = self.to_rgba(self._A, bytes=True) self._rgbacache = A if self._A.ndim == 2: self.is_grayscale = self.cmap.is_gray() else: A = self._rgbacache vl = self.axes.viewLim im = _image.pcolor2(self._Ax, self._Ay, A, height, width, (vl.x0, vl.x1, vl.y0, vl.y1), bg) return im, l, b, IdentityTransform()
return False
""" Set the grid for the rectangle boundaries, and the data values.
*x* and *y* are monotonic 1-D ndarrays of lengths N+1 and M+1, respectively, specifying rectangle boundaries. If None, they will be created as uniform arrays from 0 through N and 0 through M, respectively.
*A* is an (M,N) ndarray or masked array of values to be colormapped, or a (M,N,3) RGB array, or a (M,N,4) RGBA array.
""" A = cbook.safe_masked_invalid(A, copy=True) if x is None: x = np.arange(0, A.shape[1]+1, dtype=np.float64) else: x = np.array(x, np.float64).ravel() if y is None: y = np.arange(0, A.shape[0]+1, dtype=np.float64) else: y = np.array(y, np.float64).ravel()
if A.shape[:2] != (y.size-1, x.size-1): raise ValueError( "Axes don't match array shape. Got %s, expected %s." % (A.shape[:2], (y.size - 1, x.size - 1))) if A.ndim not in [2, 3]: raise ValueError("A must be 2D or 3D") if A.ndim == 3 and A.shape[2] == 1: A.shape = A.shape[:2] self.is_grayscale = False if A.ndim == 3: if A.shape[2] in [3, 4]: if ((A[:, :, 0] == A[:, :, 1]).all() and (A[:, :, 0] == A[:, :, 2]).all()): self.is_grayscale = True else: raise ValueError("3D arrays must have RGB or RGBA as last dim")
# For efficient cursor readout, ensure x and y are increasing. if x[-1] < x[0]: x = x[::-1] A = A[:, ::-1] if y[-1] < y[0]: y = y[::-1] A = A[::-1]
self._A = A self._Ax = x self._Ay = y self._rgbacache = None self.stale = True
raise NotImplementedError('Method not supported')
"""Get the cursor data for a given event""" x, y = event.xdata, event.ydata if (x < self._Ax[0] or x > self._Ax[-1] or y < self._Ay[0] or y > self._Ay[-1]): return None j = np.searchsorted(self._Ax, x) - 1 i = np.searchsorted(self._Ay, y) - 1 try: return self._A[i, j] except IndexError: return None
cmap=None, norm=None, offsetx=0, offsety=0, origin=None, **kwargs ): """ cmap is a colors.Colormap instance norm is a colors.Normalize instance to map luminance to 0-1
kwargs are an optional list of Artist keyword args """ super().__init__( None, norm=norm, cmap=cmap, origin=origin ) self.figure = fig self.ox = offsetx self.oy = offsety self.update(kwargs) self.magnification = 1.0
"""Get the image extent: left, right, bottom, top""" numrows, numcols = self.get_size() return (-0.5 + self.ox, numcols-0.5 + self.ox, -0.5 + self.oy, numrows-0.5 + self.oy)
fac = renderer.dpi/self.figure.dpi # fac here is to account for pdf, eps, svg backends where # figure.dpi is set to 72. This means we need to scale the # image (using magification) and offset it appropriately. bbox = Bbox([[self.ox/fac, self.oy/fac], [(self.ox/fac + self._A.shape[1]), (self.oy/fac + self._A.shape[0])]]) width, height = self.figure.get_size_inches() width *= renderer.dpi height *= renderer.dpi clip = Bbox([[0, 0], [width, height]])
return self._make_image( self._A, bbox, bbox, clip, magnification=magnification / fac, unsampled=unsampled, round_to_pixel_border=False)
"""Set the image array.""" cm.ScalarMappable.set_array(self, cbook.safe_masked_invalid(A, copy=True)) self.stale = True
"""The Image class whose size is determined by the given bbox.""" cmap=None, norm=None, interpolation=None, origin=None, filternorm=1, filterrad=4.0, resample=False, interp_at_native=True, **kwargs ): """ cmap is a colors.Colormap instance norm is a colors.Normalize instance to map luminance to 0-1
interp_at_native is a flag that determines whether or not interpolation should still be applied when the image is displayed at its native resolution. A common use case for this is when displaying an image for annotational purposes; it is treated similarly to Photoshop (interpolation is only used when displaying the image at non-native resolutions).
kwargs are an optional list of Artist keyword args
""" super().__init__( None, cmap=cmap, norm=norm, interpolation=interpolation, origin=origin, filternorm=filternorm, filterrad=filterrad, resample=resample, **kwargs )
self.bbox = bbox self.interp_at_native = interp_at_native self._transform = IdentityTransform()
return self._transform
if renderer is None: renderer = self.get_figure()._cachedRenderer
if isinstance(self.bbox, BboxBase): return self.bbox elif callable(self.bbox): return self.bbox(renderer) else: raise ValueError("unknown type of bbox")
"""Test whether the mouse event occurred within the image.""" if callable(self._contains): return self._contains(self, mouseevent)
if not self.get_visible(): # or self.get_figure()._renderer is None: return False, {}
x, y = mouseevent.x, mouseevent.y inside = self.get_window_extent().contains(x, y)
return inside, {}
width, height = renderer.get_canvas_width_height()
bbox_in = self.get_window_extent(renderer).frozen() bbox_in._points /= [width, height] bbox_out = self.get_window_extent(renderer) clip = Bbox([[0, 0], [width, height]]) self._transform = BboxTransform(Bbox([[0, 0], [1, 1]]), clip)
return self._make_image( self._A, bbox_in, bbox_out, clip, magnification, unsampled=unsampled)
""" Read an image from a file into an array.
Parameters ---------- fname : str or file-like The image file to read. This can be a filename, a URL or a Python file-like object opened in read-binary mode. format : str, optional The image file format assumed for reading the data. If not given, the format is deduced from the filename. If nothing can be deduced, PNG is tried.
Returns ------- imagedata : :class:`numpy.array` The image data. The returned array has shape
- (M, N) for grayscale images. - (M, N, 3) for RGB images. - (M, N, 4) for RGBA images.
Notes ----- Matplotlib can only read PNGs natively. Further image formats are supported via the optional dependency on Pillow. Note, URL strings are not compatible with Pillow. Check the `Pillow documentation`_ for more information.
.. _Pillow documentation: http://pillow.readthedocs.io/en/latest/ """
handlers = {'png': _png.read_png, } if format is None: if isinstance(fname, str): parsed = urllib.parse.urlparse(fname) # If the string is a URL, assume png if len(parsed.scheme) > 1: ext = 'png' else: basename, ext = os.path.splitext(fname) ext = ext.lower()[1:] elif hasattr(fname, 'name'): basename, ext = os.path.splitext(fname.name) ext = ext.lower()[1:] else: ext = 'png' else: ext = format
if ext not in handlers: # Try to load the image with PIL. try: from PIL import Image except ImportError: raise ValueError('Only know how to handle extensions: %s; ' 'with Pillow installed matplotlib can handle ' 'more images' % list(handlers)) with Image.open(fname) as image: return pil_to_array(image)
handler = handlers[ext]
# To handle Unicode filenames, we pass a file object to the PNG # reader extension, since Python handles them quite well, but it's # tricky in C. if isinstance(fname, str): parsed = urllib.parse.urlparse(fname) # If fname is a URL, download the data if len(parsed.scheme) > 1: fd = BytesIO(urllib.request.urlopen(fname).read()) return handler(fd) else: with open(fname, 'rb') as fd: return handler(fd) else: return handler(fname)
origin=None, dpi=100): """ Save an array as in image file.
The output formats available depend on the backend being used.
Parameters ---------- fname : str or file-like The filename or a Python file-like object to store the image in. The necessary output format is inferred from the filename extension but may be explicitly overwritten using *format*. arr : array-like The image data. The shape can be one of MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA). vmin, vmax : scalar, optional *vmin* and *vmax* set the color scaling for the image by fixing the values that map to the colormap color limits. If either *vmin* or *vmax* is None, that limit is determined from the *arr* min/max value. cmap : str or `~matplotlib.colors.Colormap`, optional A Colormap instance or registered colormap name. The colormap maps scalar data to colors. It is ignored for RGB(A) data. Defaults to :rc:`image.cmap` ('viridis'). format : str, optional The file format, e.g. 'png', 'pdf', 'svg', ... . If not given, the format is deduced form the filename extension in *fname*. See `.Figure.savefig` for details. origin : {'upper', 'lower'}, optional Indicates whether the ``(0, 0)`` index of the array is in the upper left or lower left corner of the axes. Defaults to :rc:`image.origin` ('upper'). dpi : int The DPI to store in the metadata of the file. This does not affect the resolution of the output image. """ from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure if isinstance(fname, getattr(os, "PathLike", ())): fname = os.fspath(fname) if (format == 'png' or (format is None and isinstance(fname, str) and fname.lower().endswith('.png'))): image = AxesImage(None, cmap=cmap, origin=origin) image.set_data(arr) image.set_clim(vmin, vmax) image.write_png(fname) else: fig = Figure(dpi=dpi, frameon=False) FigureCanvas(fig) fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin, resize=True) fig.savefig(fname, dpi=dpi, format=format, transparent=True)
"""Load a `PIL image`_ and return it as a numpy array.
.. _PIL image: https://pillow.readthedocs.io/en/latest/reference/Image.html
Returns ------- numpy.array
The array shape depends on the image type:
- (M, N) for grayscale images. - (M, N, 3) for RGB images. - (M, N, 4) for RGBA images.
""" if pilImage.mode in ['RGBA', 'RGBX', 'RGB', 'L']: # return MxNx4 RGBA, MxNx3 RBA, or MxN luminance array return np.asarray(pilImage) elif pilImage.mode.startswith('I;16'): # return MxN luminance array of uint16 raw = pilImage.tobytes('raw', pilImage.mode) if pilImage.mode.endswith('B'): x = np.fromstring(raw, '>u2') else: x = np.fromstring(raw, '<u2') return x.reshape(pilImage.size[::-1]).astype('=u2') else: # try to convert to an rgba image try: pilImage = pilImage.convert('RGBA') except ValueError: raise RuntimeError('Unknown image mode') return np.asarray(pilImage) # return MxNx4 RGBA array
preview=False): """ Make a thumbnail of image in *infile* with output filename *thumbfile*.
See :doc:`/gallery/misc/image_thumbnail_sgskip`.
Parameters ---------- infile : str or file-like The image file -- must be PNG, Pillow-readable if you have `Pillow <http://python-pillow.org/>`_ installed.
thumbfile : str or file-like The thumbnail filename.
scale : float, optional The scale factor for the thumbnail.
interpolation : str, optional The interpolation scheme used in the resampling. See the *interpolation* parameter of `~.Axes.imshow` for possible values.
preview : bool, optional If True, the default backend (presumably a user interface backend) will be used which will cause a figure to be raised if `~matplotlib.pyplot.show` is called. If it is False, the figure is created using `FigureCanvasBase` and the drawing backend is selected as `~matplotlib.figure.savefig` would normally do.
Returns ------- figure : `~.figure.Figure` The figure instance containing the thumbnail. """
im = imread(infile) rows, cols, depth = im.shape
# This doesn't really matter (it cancels in the end) but the API needs it. dpi = 100
height = rows / dpi * scale width = cols / dpi * scale
if preview: # Let the UI backend do everything. import matplotlib.pyplot as plt fig = plt.figure(figsize=(width, height), dpi=dpi) else: from matplotlib.figure import Figure fig = Figure(figsize=(width, height), dpi=dpi) FigureCanvasBase(fig)
ax = fig.add_axes([0, 0, 1, 1], aspect='auto', frameon=False, xticks=[], yticks=[]) ax.imshow(im, aspect='auto', resample=True, interpolation=interpolation) fig.savefig(thumbfile, dpi=dpi) return fig |