Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1290594

Browse files
committed
Clip RGB data to vaild range in Axes.imshow
1 parent f6ebbc3 commit 1290594

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

doc/users/credits.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ Yu Feng,
386386
Yunfei Yang,
387387
Yuri D'Elia,
388388
Yuval Langer,
389+
Zac Hatfield-Dodds,
389390
Zach Pincus,
390391
Zair Mubashar,
391392
alex,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
`Axes.imshow` clips RGB values to the valid range
2+
-------------------------------------------------
3+
4+
When `Axes.imshow` is passed an RGB or RGBA value with out-of-range
5+
values, it now issues a warning and clips them to the valid range.
6+
The old behaviour, wrapping back in to the range, often hid outliers
7+
and made interpreting RGB images unreliable.

lib/matplotlib/axes/_axes.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5228,10 +5228,14 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
52285228
- MxNx3 -- RGB (float or uint8)
52295229
- MxNx4 -- RGBA (float or uint8)
52305230
5231-
The value for each component of MxNx3 and MxNx4 float arrays
5232-
should be in the range 0.0 to 1.0. MxN arrays are mapped
5233-
to colors based on the `norm` (mapping scalar to scalar)
5234-
and the `cmap` (mapping the normed scalar to a color).
5231+
MxN arrays are mapped to colors based on the `norm` (mapping
5232+
scalar to scalar) and the `cmap` (mapping the normed scalar to
5233+
a color).
5234+
5235+
Elements of RGB and RGBA arrays represent pixels of an MxN image.
5236+
All values should be in the range [0 .. 1] for floats or
5237+
[0 .. 255] for integers. Out-of-range values will be clipped to
5238+
these bounds.
52355239
52365240
cmap : `~matplotlib.colors.Colormap`, optional, default: None
52375241
If None, default to rc `image.cmap` value. `cmap` is ignored
@@ -5273,7 +5277,8 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
52735277
settings for `vmin` and `vmax` will be ignored.
52745278
52755279
alpha : scalar, optional, default: None
5276-
The alpha blending value, between 0 (transparent) and 1 (opaque)
5280+
The alpha blending value, between 0 (transparent) and 1 (opaque).
5281+
The ``alpha`` argument is ignored for RGBA input data.
52775282
52785283
origin : ['upper' | 'lower'], optional, default: None
52795284
Place the [0,0] index of the array in the upper left or lower left

lib/matplotlib/cm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
259259
xx = (xx * 255).astype(np.uint8)
260260
elif xx.dtype == np.uint8:
261261
if not bytes:
262-
xx = xx.astype(float) / 255
262+
xx = xx.astype(np.float32) / 255
263263
else:
264264
raise ValueError("Image RGB array must be uint8 or "
265265
"floating point; found %s" % xx.dtype)

lib/matplotlib/image.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from math import ceil
1515
import os
16+
import warnings
1617

1718
import numpy as np
1819

@@ -610,6 +611,23 @@ def set_data(self, A):
610611
or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
611612
raise TypeError("Invalid dimensions for image data")
612613

614+
if self._A.ndim == 3:
615+
# If the input data has values outside the valid range (after
616+
# normalisation), we issue a warning and then clip X to the bounds
617+
# - otherwise casting wraps extreme values, hiding outliers and
618+
# making reliable interpretation impossible.
619+
high = 255 if np.issubdtype(self._A.dtype, np.integer) else 1
620+
if self._A.min() < 0 or high < self._A.max():
621+
warnings.warn(
622+
'Clipping input data to the valid range for imshow with '
623+
'RGB data ([0..1] for floats or [0..255] for integers).'
624+
)
625+
self._A = np.clip(self._A, 0, high)
626+
# Cast unsupported integer types to uint8
627+
if self._A.dtype != np.uint8 and np.issubdtype(self._A.dtype,
628+
np.integer):
629+
self._A = self._A.astype(np.uint8)
630+
613631
self._imcache = None
614632
self._rgbacache = None
615633
self.stale = True

0 commit comments

Comments
 (0)