gpt4 book ai didi

python - 使用 pyqtplot 绘制从 .tiff 图像构建的 numpy 数组

转载 作者:行者123 更新时间:2023-11-30 23:29:36 25 4
gpt4 key购买 nike

我正在尝试在 pyqtgraph 中绘制 tiff 图像。

import numpy as np
import gdal
import pyqtgraph as pg
from PyQt4 import QtCore

gd = gdal.Open('myImage.tif')
data = np.array(gd.GetRasterBand(1).ReadAsArray())
pg.plot(data, title="my picture")

if __name__ == '__main__':
import sys
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
pg.QtGui.QApplication.exec_()

我收到错误

Traceback (most recent call last):
File "C:/Users/justin/PycharmProjects/pyqtgraph_examples/geotiff.py", line 18, in <module>
pg.plot(data, title="my picture")
File "C:\Python33\lib\site-packages\pyqtgraph\__init__.py", line 295, in plot
w.plot(*args, **dataArgs)
File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\PlotItem\PlotItem.py", line 639, in plot
item = PlotDataItem(*args, **kargs)
File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 165, in __init__
self.setData(*args, **kargs)
File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 341, in setData
dt = dataType(data)
File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 679, in dataType
raise Exception('array shape must be (N,) or (N,2); got %s instead' % str(obj.shape))
Exception: array shape must be (N,) or (N,2); got (788, 744) instead

print(data.size) 返回 (788,744)。

我认为我的 numpy 数组格式错误,或者我使用了错误的 pyqtgraph 函数,但我对这两者都不熟悉,不知道下一步要尝试什么。

最佳答案

我想你想要pyqtgraph.image 。例如,这是脚本的修改版本(我安装了 PySide):

import numpy as np
import pyqtgraph as pg
from PySide import QtCore
from scipy.ndimage import gaussian_filter


data = np.random.beta(0.5, 3, size=(500, 500))
data = gaussian_filter(data, sigma=(12, 3))

pg.image(data, title="my picture")

if __name__ == '__main__':
import sys
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
pg.QtGui.QApplication.exec_()

enter image description here

关于python - 使用 pyqtplot 绘制从 .tiff 图像构建的 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21069716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com