Issue
I'm facing an issue where the colormap of a pyqtgraph ImageItem
seems to be reset to grayscale when the widget is being resized. Consider the following code
# PyQt6==6.4.2
# pyqtgraph==0.13.3
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
# Interpret image data as row-major instead of col-major
pg.setConfigOptions(imageAxisOrder='row-major')
pg.mkQApp()
win = pg.GraphicsLayoutWidget()
# A plot area (ViewBox + axes) for displaying the image
p1 = win.addPlot(title="")
# Item for displaying image data
img = pg.ImageItem()
p1.addItem(img)
# Contrast/color control
hist = pg.HistogramLUTItem()
hist.setImageItem(img) # removing this line prevents the colorMap from changing when resizing the window
win.addItem(hist)
# Generate image data
data = np.random.normal(size=(200, 100))
img.setImage(data)
img.setColorMap(pg.colormap.get('CET-R4'))
hist.setLevels(data.min(), data.max())
win.show()
if __name__ == '__main__':
pg.exec()
When running this, the window appears as expected with the 'CET-R4' colormap:
However, as soon as I resize the window, the image turns into grayscale:
I think it has to do with the HistogramLUTItem
, since the same behaviour appears when sliding the gradient editor in the HistogramLUTItem
.
If the hist.setImageItem(img)
line is removed when running the code and the HistogramLUTItem
was never linked to the ImageItem()
, the colormap remains unchanged.
Am I doing something wrong or is it a bug? Any ideas on how to fix or work around it?
Solution
The HistogramLUTItem
is designed to control the LUT of the target image. So it's not a good idea to set the LUT of the image manually.
But if you insist, you can do like this using the sigLookupTableChanged
signal. But it may be broken in the future.
...
hist = pg.HistogramLUTItem()
#hist.setImageItem(img)
hist.sigLookupTableChanged.connect(
lambda: img.setColorMap(pg.colormap.get('CET-R4')))
...
Answered By - relent95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.