gpt4 book ai didi

pyqt - 画线: retrieve the covered pixels

转载 作者:行者123 更新时间:2023-12-02 04:39:56 24 4
gpt4 key购买 nike

我想在一个小部件上画一条线:

from PyQt4 import QtGui, QtCore

class LineLabel(QtGui.QLabel):

def __init__(self,parent=None):
super(LineLabel,self).__init__(parent)
self.setMinimumSize(100,100)
self.setMaximumSize(100,100)

def paintEvent(self,e):
painter=QtGui.QPainter(self)
pen = QtGui.QPen()
pen.setWidth(5)
painter.setPen(pen)
painter.drawLine(10,10,90,90)
painter.end()

def test():
form = QtGui.QWidget()
label = LineLabel(form)
form.show()
return form

import sys
app = QtGui.QApplication(sys.argv)
window =test()
sys.exit(app.exec_())

获取直线覆盖的像素列表的最佳方法是什么?

评论更新:

  • 我不需要直接知道起点和终点之间的像素,而是所有那些变成黑色的像素(这些像素更多,因为线有一定的宽度)。
  • 我的总体目标是快速了解小部件上的哪些像素是黑色的。遍历图像的像素并查询颜色比从存储颜色的列表中读取颜色值要慢得多:对我来说,100 万像素的图像需要 1.9 秒,100 万个条目的列表需要 0.23 秒.因此,每次更改小部件上的图像(例如画一条线)后,我都必须更新该列表。
  • 在 QGraphicsScene 中引用 QGraphicsItem 的答案也很有帮助。

最佳答案

您可以使用线性方程在直线中找到您想要的点。我认为没有提及画线。

from PyQt4 import QtGui
from PyQt4.QtGui import QColor, QPaintEvent

m_nInitialX = 0.0
m_nInitialY = 0.0


# my line abstraction
class MyLine:
x1, y1, x2, y2 = .0, .0, .0, .0
width = .0
px, py = (.0, .0)
draw_point = False

def __init__(self, x1, y1, x2, y2, width):
self.x1, self.y1, self.x2, self.y2 = (x1, y1, x2, y2)
self.width = width

def is_in_line(self, x, y):
# mark a position in the line
m = (self.y2 - self.y1) / (self.x2 - self.x1)
print(m*(x-self.x1)-(y-self.y1))
if abs((m*(x-self.x1) - (y-self.y1))) <= self.width/2:
self.draw_point = True
return True
else:
return False

def add_red_point(self, x, y):
self.px, self.py = (x, y)

def draw(self, widget):
painter = QtGui.QPainter(widget)
pen = QtGui.QPen()
pen.setWidth(self.width)
painter.setPen(pen)
painter.drawLine(self.x1, self.y1, self.y2, self.y2)

if self.draw_point:
pen.setColor(QColor(255, 0, 0))
painter.setPen(pen)
painter.drawPoint(self.px, self.py)
painter.end()


line = MyLine(10, 10, 90, 90, width=10) # <-- my line abstraction


class LineLabel(QtGui.QLabel):

def __init__(self, parent=None):
super(LineLabel, self).__init__(parent)
self.setMinimumSize(100, 100)
self.setMaximumSize(100, 100)

# always redraw when needed
def paintEvent(self, e):
print("draw!")
line.draw(self)

def mousePressEvent(self, event):
# mark clicked position in line
m_nInitialX = event.pos().x()
m_nInitialY = event.pos().y()
if line.is_in_line(m_nInitialX, m_nInitialY):
line.add_red_point(m_nInitialX, m_nInitialY)
self.repaint()


def test():
form = QtGui.QWidget()
label = LineLabel(form)
form.show()
return form

import sys

app = QtGui.QApplication(sys.argv)
window = test()
sys.exit(app.exec_())

关于pyqt - 画线: retrieve the covered pixels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38446136/

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