2010-03-22 06:21:56 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-25 04:09:03 +00:00
|
|
|
"""
|
|
|
|
Demonstrates very basic use of ImageItem to display image data inside a ViewBox.
|
|
|
|
"""
|
|
|
|
|
2010-03-22 06:21:56 +00:00
|
|
|
## Add path to library (just for examples; you do not need this)
|
2012-12-26 22:51:52 +00:00
|
|
|
import initExample
|
2011-04-05 14:35:50 +00:00
|
|
|
|
2012-03-02 03:58:02 +00:00
|
|
|
from pyqtgraph.Qt import QtCore, QtGui
|
2011-04-05 14:35:50 +00:00
|
|
|
import numpy as np
|
|
|
|
import pyqtgraph as pg
|
2021-07-23 03:57:50 +00:00
|
|
|
from time import perf_counter
|
2010-03-22 06:21:56 +00:00
|
|
|
|
2021-01-27 18:59:07 +00:00
|
|
|
app = pg.mkQApp("ImageItem Example")
|
2010-03-22 06:21:56 +00:00
|
|
|
|
|
|
|
## Create window with GraphicsView widget
|
2013-02-25 04:09:03 +00:00
|
|
|
win = pg.GraphicsLayoutWidget()
|
|
|
|
win.show() ## show widget alone in its own window
|
|
|
|
win.setWindowTitle('pyqtgraph example: ImageItem')
|
|
|
|
view = win.addViewBox()
|
2010-03-22 06:21:56 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
## lock the aspect ratio so pixels are always square
|
2010-03-22 06:21:56 +00:00
|
|
|
view.setAspectLocked(True)
|
|
|
|
|
|
|
|
## Create image item
|
2012-03-02 02:55:32 +00:00
|
|
|
img = pg.ImageItem(border='w')
|
2013-02-25 04:09:03 +00:00
|
|
|
view.addItem(img)
|
2010-03-22 06:21:56 +00:00
|
|
|
|
|
|
|
## Set initial view bounds
|
2012-03-02 02:55:32 +00:00
|
|
|
view.setRange(QtCore.QRectF(0, 0, 600, 600))
|
2010-03-22 06:21:56 +00:00
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
## Create random image
|
2012-03-02 02:55:32 +00:00
|
|
|
data = np.random.normal(size=(15, 600, 600), loc=1024, scale=64).astype(np.uint16)
|
2011-04-05 14:35:50 +00:00
|
|
|
i = 0
|
|
|
|
|
2021-07-23 03:57:50 +00:00
|
|
|
updateTime = perf_counter()
|
|
|
|
elapsed = 0
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2021-03-01 05:45:00 +00:00
|
|
|
timer = QtCore.QTimer()
|
|
|
|
timer.setSingleShot(True)
|
|
|
|
# not using QTimer.singleShot() because of persistence on PyQt. see PR #1605
|
|
|
|
|
2010-03-22 06:21:56 +00:00
|
|
|
def updateData():
|
2021-07-23 03:57:50 +00:00
|
|
|
global img, data, i, updateTime, elapsed
|
2010-03-22 06:21:56 +00:00
|
|
|
|
|
|
|
## Display the data
|
2012-03-02 02:55:32 +00:00
|
|
|
img.setImage(data[i])
|
2011-04-05 14:35:50 +00:00
|
|
|
i = (i+1) % data.shape[0]
|
2011-06-14 23:47:52 +00:00
|
|
|
|
2021-03-01 05:45:00 +00:00
|
|
|
timer.start(1)
|
2021-07-23 03:57:50 +00:00
|
|
|
now = perf_counter()
|
|
|
|
elapsed_now = now - updateTime
|
2012-03-02 02:55:32 +00:00
|
|
|
updateTime = now
|
2021-07-23 03:57:50 +00:00
|
|
|
elapsed = elapsed * 0.9 + elapsed_now * 0.1
|
|
|
|
|
|
|
|
# print(f"{1 / elapsed:.1f} fps")
|
2010-03-22 06:21:56 +00:00
|
|
|
|
2021-03-01 05:45:00 +00:00
|
|
|
timer.timeout.connect(updateData)
|
2011-06-14 23:47:52 +00:00
|
|
|
updateData()
|
|
|
|
|
2012-12-05 05:25:45 +00:00
|
|
|
if __name__ == '__main__':
|
2021-05-13 21:28:22 +00:00
|
|
|
pg.exec()
|