如何在不保存图像其余部分的情况下使用 QPainterPath 裁剪图像
How to crop an image using QPainterPath without saving rest of image
我有一个 QPainterPath,我想裁剪一张 QPixmap 图像。
此代码对我有用,但我想使用 PyQt5 内置功能
像没有 numpy 的面具
# 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")
一种替换蒙版的可能方法是使用 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')
根据上面的回答,我稍微调整了我的代码,现在它看起来像:
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')
我有一个 QPainterPath,我想裁剪一张 QPixmap 图像。 此代码对我有用,但我想使用 PyQt5 内置功能 像没有 numpy 的面具
# 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")
一种替换蒙版的可能方法是使用 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')
根据上面的回答,我稍微调整了我的代码,现在它看起来像:
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')