我是Pyqt的新手,我正在嘗試創建一個在我們給出像素位置時在圖像上打印一個圓(使用QPainter)的UI(QLabel)。對於測試,我使用鼠標跟蹤事件,但我想我有圖像或標籤調整大小的問題。下面從我實現圖像:如何在PyQt5上正確設置Pixmap的縮放比例?
結果:
看看指針和圓的位置。這是我想要解決的。
下面你可以看到代碼(我使用PIL(加載圖像)和PyQt5):
...
self.image = Image.open("Images/Quart_top_View_draw.jpg")
self.MainWindow.TennisCourtImage.setMouseTracking(True)
self.MainWindow.TennisCourtImage.mouseMoveEvent = self.get_mouse_pos
def get_mouse_pos(self,event):
x = event.pos().x()
y = event.pos().y()
print("Position: ", x,y)
img = self.image
width, height = img.size
print("Image Size: ", width, height)
img = ImageQt(img)
pixmap_image = QtGui.QPixmap.fromImage(img)
self.painterInstance = QtGui.QPainter(pixmap_image)
self.painterInstance.setPen(QPen(Qt.red, 15, Qt.SolidLine))
self.painterInstance.drawEllipse(x,y,15,15)
self.painterInstance.end()
myScaledPixmap = pixmap_image.scaled(self.MainWindow.TennisCourtImage.size(), Qt.KeepAspectRatio)
self.MainWindow.TennisCourtImage.setPixmap(myScaledPixmap)
...
有人可以幫助我,好嗎?
問候,
百利:d
爲什麼使用PIL? – eyllanesc
你的問題是什麼?你想得到什麼? – eyllanesc
PIL沒有必要。我可以改變我在哪裏使用PIL的線\t \t pixmap_image = QtGui.QPixmap(「Images/Quart_top_View_draw.jpg」)。我需要的是繪製我的指針所在的圓圈。 –