我有一个qpanterpath,我想裁剪一个QPixmap图像。
这段代码对我有效,但我想使用PyQt5内置功能
就像没有纽比的面具

# read image as RGB and add alpha (transparency)
im = Image.open("frontal_1.jpg").convert("RGBA")

# convert to numpy (for convenience)
imArray = numpy.asarray(im)

# create mask
polygon = [(444, 203), (623, 243), (691, 177), (581, 26), (482, 42)]

maskIm = Image.new('L', (imArray.shape[1], imArray.shape[0]), 0)
ImageDraw.Draw(maskIm).polygon(polygon, outline=1, fill=1)
mask = numpy.array(maskIm)
...
newIm = Image.fromarray(newImArray, "RGBA")
newIm.save("out.png")

最佳答案

替换mask的一种可能方法是使用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')

关于python - 如何使用QPainterPath裁剪图像而不保存其余图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55354509/

10-12 18:37