Last active
December 29, 2016 16:18
-
-
Save libbkmz/6a801dbcdb7ed324ba866418eb5cb3ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Demonstrates very basic use of ImageItem to display image data inside a ViewBox. | |
""" | |
## Add path to library (just for examples; you do not need this) | |
# import initExample | |
from pyqtgraph.Qt import QtCore, QtGui | |
import numpy as np | |
import pyqtgraph as pg | |
from pyqtgraph.GraphicsScene.mouseEvents import MouseClickEvent | |
import pyqtgraph.ptime as ptime | |
W = WIDTH = 600 | |
H = HEIGHT = 600 | |
app = QtGui.QApplication([]) | |
# Create window with GraphicsView widget | |
win = pg.GraphicsLayoutWidget() | |
win.resize(W, H) | |
win.show() # show widget alone in its own window | |
win.setWindowTitle('pyqtgraph example: ImageItem') | |
view = win.addViewBox() # type: pg.ViewBox | |
# lock the aspect ratio so pixels are always square | |
view.setAspectLocked(True) | |
# Create image item | |
img = pg.ImageItem(border='w') | |
view.addItem(img) | |
data = np.zeros(shape=(H, W), dtype=np.uint8) | |
img.setImage(data) | |
img.setLevels((0, 1)) | |
pos = 1200 | |
updateTime = ptime.time() | |
fps = 0 | |
updateTime = ptime.time() | |
def get_xy(pos): | |
return pos // W, pos % H | |
def updater(): | |
global data, img, pos, updateTime, fps | |
for _ in xrange(50): | |
data[get_xy(pos)] = 1 | |
pos += 1 | |
img.setImage(data.T) | |
now = ptime.time() | |
fps2 = 1.0 / (now-updateTime) | |
updateTime = now | |
fps = fps * 0.9 + fps2 * 0.1 | |
print "%0.1f fps" % fps | |
# QtCore.QTimer.singleShot(1, updater) | |
timer = pg.QtCore.QTimer() | |
timer.timeout.connect(updater) | |
timer.start(0) | |
def mouseMoved(e): | |
""" | |
:type e: MouseClickEvent | |
""" | |
global img, data | |
print e | |
# x, y = e.scenePos().x(), e.scenePos().y() | |
x, y = e.pos().x(), e.pos().y() | |
x, y = int(x), H-int(y) | |
print x, y | |
# x = np.arange(x-2, x+2) | |
# y = np.arange(y-2, y+2) | |
data[x, y] = 1 | |
print data | |
img.setImage(data, levels=(0, 1)) | |
view.scene().sigMouseClicked.connect(mouseMoved) | |
# img.scene().sigMouseMoved.connect(mouseMoved) | |
# Start Qt event loop unless running in interactive mode. | |
if __name__ == '__main__': | |
import sys | |
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): | |
QtGui.QApplication.instance().exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment