我有一个QPainterPath,我想裁剪一个QPixmap图像。 这段代码对我有用,但是我想使用PyQt5内置功能 就像没有numpy的面具
with
答案 0 :(得分:1)
替换遮罩的一种可能方法是使用QPainter setClipPath:
from PyQt5 import QtCore, QtGui
if __name__ == '__main__':
image = QtGui.QImage('input.png')
output = QtGui.QImage(image.size(), QtGui.QImage.Format_ARGB32)
output.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(output)
points = [(444, 203), (623, 243), (691, 177), (581, 26), (482, 42)]
polygon = QtGui.QPolygonF([QtCore.QPointF(*point) for point in points])
path = QtGui.QPainterPath()
path.addPolygon(polygon)
painter.setClipPath(path)
painter.drawImage(QtCore.QPoint(), image)
painter.end()
output.save('out.png')
答案 1 :(得分:0)
从上面回答后,我对代码进行了一些调整,现在看起来像这样:
path = lips_contour_path
image = QImage('frontal_2.jpg')
output = QImage(image.size(), QImage.Format_ARGB32)
output.fill(Qt.transparent)
painter = QPainter(output)
painter.setClipPath(path)
painter.drawImage(QPoint(), image)
painter.end()
# To avoid useless transparent background you can crop it like that:
output = output.copy(path.boundingRect().toRect())
output.save('out.png')