Check validity of image-like input *A* and normalize it to a format suitable for Image subclasses.
(A)
| 663 | |
| 664 | @staticmethod |
| 665 | def _normalize_image_array(A): |
| 666 | """ |
| 667 | Check validity of image-like input *A* and normalize it to a format suitable for |
| 668 | Image subclasses. |
| 669 | """ |
| 670 | A = cbook.safe_masked_invalid(A, copy=True) |
| 671 | if A.dtype != np.uint8 and not np.can_cast(A.dtype, float, "same_kind"): |
| 672 | raise TypeError(f"Image data of dtype {A.dtype} cannot be " |
| 673 | f"converted to float") |
| 674 | if A.ndim == 3 and A.shape[-1] == 1: |
| 675 | A = A.squeeze(-1) # If just (M, N, 1), assume scalar and apply colormap. |
| 676 | if not (A.ndim == 2 or A.ndim == 3 and A.shape[-1] in [3, 4]): |
| 677 | raise TypeError(f"Invalid shape {A.shape} for image data") |
| 678 | if A.ndim == 3: |
| 679 | # If the input data has values outside the valid range (after |
| 680 | # normalisation), we issue a warning and then clip X to the bounds |
| 681 | # - otherwise casting wraps extreme values, hiding outliers and |
| 682 | # making reliable interpretation impossible. |
| 683 | high = 255 if np.issubdtype(A.dtype, np.integer) else 1 |
| 684 | if A.min() < 0 or high < A.max(): |
| 685 | _log.warning( |
| 686 | 'Clipping input data to the valid range for imshow with ' |
| 687 | 'RGB data ([0..1] for floats or [0..255] for integers). ' |
| 688 | 'Got range [%s..%s].', |
| 689 | A.min(), A.max() |
| 690 | ) |
| 691 | A = np.clip(A, 0, high) |
| 692 | # Cast unsupported integer types to uint8 |
| 693 | if A.dtype != np.uint8 and np.issubdtype(A.dtype, np.integer): |
| 694 | A = A.astype(np.uint8) |
| 695 | return A |
| 696 | |
| 697 | def set_data(self, A): |
| 698 | """ |