Pyqt5鼠标点击获取图像坐标
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
layout = QtWidgets.QGridLayout(self)
self.getImageButton = QtWidgets.QPushButton('Select')
layout.addWidget(self.getImageButton)
self.getImageButton.clicked.connect(self.resimac)
self.resim1 = QtWidgets.QLabel()
layout.addWidget(self.resim1)
self.resim1.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignVCenter)
# I'm assuming the following...
self.resim1.setScaledContents(True)
self.resim1.setFixedSize(701,451)
# install an event filter to "capture" mouse events (amongst others)
self.resim1.installEventFilter(self)
def resimac(self):
filename, filter = QtWidgets.QFileDialog.getOpenFileName(None, 'Resim Yükle', '.', 'Image Files (*.png *.jpg *.jpeg *.bmp *.tif)')
if not filename:
return
self.resim1.setPixmap(QtGui.QPixmap(filename))
def eventFilter(self, source, event):
# if the source is our QLabel, it has a valid pixmap, and the event is
# a left click, proceed in trying to get the event position
if (source == self.resim1 and source.pixmap() and not source.pixmap().isNull() and
event.type() == QtCore.QEvent.MouseButtonPress and
event.button() == QtCore.Qt.LeftButton):
self.getClickedPosition(event.pos())
return super().eventFilter(source, event)
def getClickedPosition(self, pos):
# consider the widget contents margins
contentsRect = QtCore.QRectF(self.resim1.contentsRect())
if pos not in contentsRect:
# outside widget margins, ignore!
return
# adjust the position to the contents margins
pos -= contentsRect.topLeft()
pixmapRect = self.resim1.pixmap().rect()
if self.resim1.hasScaledContents():
x = pos.x() * pixmapRect.width() / contentsRect.width()
y = pos.y() * pixmapRect.height() / contentsRect.height()
pos = QtCore.QPoint(x, y)
else:
align = self.resim1.alignment()
# for historical reasons, QRect (which is based on integer values),
# returns right() as (left+width-1) and bottom as (top+height-1),
# and so their opposite functions set/moveRight and set/moveBottom
# take that into consideration; using a QRectF can prevent that; see:
# https://doc.qt.io/qt-5/qrect.html#right
# https://doc.qt.io/qt-5/qrect.html#bottom
pixmapRect = QtCore.QRectF(pixmapRect)
# the pixmap is not left aligned, align it correctly
if align & QtCore.Qt.AlignRight:
pixmapRect.moveRight(contentsRect.x() + contentsRect.width())
elif align & QtCore.Qt.AlignHCenter:
pixmapRect.moveLeft(contentsRect.center().x() - pixmapRect.width() / 2)
# the pixmap is not top aligned (note that the default for QLabel is
# Qt.AlignVCenter, the vertical center)
if align & QtCore.Qt.AlignBottom:
pixmapRect.moveBottom(contentsRect.y() + contentsRect.height())
elif align & QtCore.Qt.AlignVCenter:
pixmapRect.moveTop(contentsRect.center().y() - pixmapRect.height() / 2)
if not pos in pixmapRect:
# outside image margins, ignore!
return
# translate coordinates to the image position and convert it back to
# a QPoint, which is integer based
pos = (pos - pixmapRect.topLeft()).toPoint()
print('X={}, Y={}'.format(pos.x(), pos.y()))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
Pyqt5拖拽图片显示到lable上
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import cv2
import numpy as np
from ui.ui import Ui_MainWindow
import os
class tuodong(QtWidgets.QLabel):
sinh = QtCore.pyqtSignal(str)
def __init__(self):
super(tuo,self).__init__()
self.setAcceptDrops(True)
self.tu=""
# # 鼠标拖入事件
def dragEnterEvent(self, evn):
# 鼠标放开函数事件
evn.accept()
# 鼠标放开执行
def dropEvent(self, evn):
a=os.path.splitext(evn.mimeData().text())[1]
self.tu =evn.mimeData().text()[8:]
if a==".jpg" or a==".png" or a==".jpeg" or a==".JPG" or a==".PNG":
#读取图片路径(支持中文路径)
self.img_yuantu =cv2.imdecode(np.fromfile(evn.mimeData().text()[8:],dtype=np.uint8),-1)
#自适应lable
if self.img_yuantu.shape[0] < self.img_yuantu.shape[1]:
width = self.width()
height = int(self.img_yuantu.shape[0] * (width / self.img_yuantu.shape[1]))
else:
height = self.height()
width = int(self.img_yuantu.shape[1] * (height / self.img_yuantu.shape[0]))
self.img_yuan = cv2.resize(self.img_yuantu,(width,height))
self.img_yuan = cv2.cvtColor(self.img_yuan,cv2.COLOR_BGR2RGBA)
showImage = QtGui.QImage(self.img_yuan.data,self.img_yuan.shape[1],self.img_yuan.shape[0],QtGui.QImage.Format_RGBA8888) #把读取到的视频数据变成QImage形式
self.setPixmap(QtGui.QPixmap.fromImage(showImage)) #往显示视频的Label里 显示QImage
self.setAlignment(QtCore.Qt.AlignCenter)#居中
def emit(self):
text =self.tu
return text
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self):
super(MainWindow,self).__init__()
self.setupUi(self)
self.img1 = tuodong()
self.img1.setAlignment(QtCore.Qt.AlignCenter)
self.img1.setObjectName("img1")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = MainWindow()
ui.show()
sys.exit(app.exec_())
本文介绍了一个使用PyQt5实现的应用程序,该程序能够通过鼠标点击获取图像上的坐标,并展示了如何处理图像文件的加载及显示。此外,还提供了一个支持拖放图像并自动调整大小以适应标签的示例。

7150

被折叠的 条评论
为什么被折叠?



