我正在使用python和Qt Designer来实现加载tiff图像并启用平移和缩放某些鼠标事件(滚轮缩放,按下滚轮 - 平移) .
我正在研究一些可以处理图像等的选项和类,到目前为止我发现:
QGraphicsScene,QImage,QGraphicsView
我有三个班(只是测试)
具有QGraphicsView元素的
- ViewerDemo:
"""description of class"""
# Form implementation generated from reading ui file 'GraphicsViewdemo.ui'
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(("Dialog"))
Dialog.resize(500, 500)
self.graphicsView = QtGui.QGraphicsView(Dialog)
self.graphicsView.setGeometry(QtCore.QRect(0, 0, 500, 500))
self.graphicsView.setObjectName(("graphicsView"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None,
QtGui.QApplication.UnicodeUTF8))
- MyForm类,即QDialog,我调用类ViewerDemo,加载Image,并将图像放入QGraphicsView
import sys
from ViewerDemo import *
from PyQt4 import QtGui
class MyForm(QtGui.QDialog):
def __init__(self, url, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.scene = QtGui.QGraphicsScene(self)
self.image = QtGui.QImage(url)
pixmap= QtGui.QPixmap.fromImage(self.image)
item=QtGui.QGraphicsPixmapItem(pixmap)
self.scene.addItem(item)
self.ui.graphicsView.setScene(self.scene)
self.scale = 1
QtCore.QObject.connect(self.scene, QtCore.SIGNAL('mousePressEvent()'),self.mousePressEvent)
def mousePressEvent(self, event):
print ('PRESSED : ',event.pos())
(3)正是应用程序执行的地方:
from PyQt4 import QtGui, QtCore
import sys
from MyForm import MyForm
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
url = "D:/probaTiff"
myapp = MyForm(url)
myapp.show()
sys.exit(app.exec_())
我发现如何在鼠标单击(左键和滚轮点击)上执行某些操作,以打印像素坐标(例如,我需要在坐标系统WGS84中获取坐标) .
我需要的更多,是如何缩放图片(滚轮或双击,无论如何)和平移图片(按住鼠标左键或保持轮) .
或者,有没有更好的Qt课程来做这个,还有一些更好的方法你能帮我吗?
这是我到目前为止使用此代码
2 回答
使用
QGraphicsView
的内置功能并不难 .下面的演示脚本具有左键平移和滚轮缩放(包括锚定到当前光标位置) .
fitInView
方法已重新实现,因为内置版本添加了一个无法删除的奇怪固定边距 .PyQt4版本:
PyQt5版本:
使用普通的PIL(枕头)库可以打开高达几千兆字节的TIFF文件 . 它不是很容易,但它确实有效 .
你可以看到example here,第二个例子是粗体 EDIT 字符串可以打开,移动和缩放TIFF文件 .