Skip to content

Commit 5186833

Browse files
committed
Try and clear up more confusion about levels
1 parent ff8599f commit 5186833

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

doc/source/api_reference/graphicsItems/imageitem.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,9 @@ The following guidance should be observed if performance is an important factor
5555
* Use C-contiguous image data.
5656
* For 1 or 3 channel data, use `uint8`, `uint16`, `float32`, or `float64`
5757
``image`` dtype.
58-
* For 4-channel data, use `uint8` or `uint16` with :python":`levels=None`.
59-
* ``levels`` should be single channel (if 1 or 3 channel data).
60-
61-
* Setting :python:`levels=None` will trigger autoLevels sampling, and thus should
62-
be avoided if possible.
63-
58+
* For 4-channel data, use `uint8` or `uint16` with :python:`levels=None`.
59+
* ``levels`` should be set to (even if to ``None``) to avoid autoLevels sampling.
60+
* ``levels`` should be single channel or ``None``.
6461
* If using LUTs (lookup tables), ensure they have a dtype of `uint8` and have 256
6562
points or less, and that. That can be accomplished with calling:
6663

pyqtgraph/graphicsItems/ImageItem.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,23 @@ def boundingRect(self) -> QtCore.QRectF:
144144
return QtCore.QRectF(0., 0., 0., 0.)
145145
return QtCore.QRectF(0., 0., float(self.width()), float(self.height()))
146146

147-
def setLevels(self, levels: npt.ArrayLike, update: bool=True):
147+
def setLevels(self, levels: npt.ArrayLike | None, update: bool=True):
148148
"""
149-
Sets image scaling levels.
149+
Sets image scaling levels. Calling this method, even with ``levels=None`` will
150+
disable auto leveling (set ``autoLevels=False``).
150151
151152
Parameters
152153
----------
153-
levels : array_like
154+
levels : array_like or None
154155
- ``[blackLevel, whiteLevel]``
155156
sets black and white levels for monochrome data and can be used with a
156157
lookup table.
157158
- ``[[minR, maxR], [minG, maxG], [minB, maxB]]``
158159
sets individual scaling for RGB values. Not compatible with lookup tables.
160+
- ``None``
161+
Disables the application of levels, but setting to ``None`` prevents
162+
the auto-levels mechanism from sampling the image. Not compatible with
163+
images that use floating point dtypes.
159164
update : bool, optional
160165
Controls if image immediately updates to reflect the new levels, default True
161166
@@ -173,10 +178,11 @@ def setLevels(self, levels: npt.ArrayLike, update: bool=True):
173178
if update:
174179
self.updateImage()
175180

176-
def getLevels(self):
181+
def getLevels(self) -> numpy.ndarray | None:
177182
"""
178-
Returns the list representing the current level settings. See :func:`setLevels`.
179-
When ``autoLevels`` is active, the format is ``[blackLevel, whiteLevel]``.
183+
Returns the array representing the current level settings. See
184+
:func:`setLevels`. When ``autoLevels`` is active, the format is
185+
``[blackLevel, whiteLevel]``.
180186
"""
181187
return self.levels
182188

@@ -503,7 +509,7 @@ def setImage(
503509
if mn == mx or self._xp.isnan(mn) or self._xp.isnan(mx):
504510
mn = 0
505511
mx = 255
506-
kargs['levels'] = [mn,mx]
512+
kargs['levels'] = self._xp.asarray((mn,mx))
507513

508514
profile()
509515

0 commit comments

Comments
 (0)