Python 带for循环的PyQt QImage边界半径

Python 带for循环的PyQt QImage边界半径,python,math,pyqt,border,qimage,Python,Math,Pyqt,Border,Qimage,我有一个矩形PNG,我想处理这个图像,删除一些像素的空白,并保存为PNG格式 这是我最后想要的 正常图像(没有边界-边界半径): 这是我的密码;我尝试了一些事情,但没有正常工作 qimg = QImage(PNG_YOL) qimg = qimg.convertToFormat(QImage.Format_ARGB32) # making png or will be there black pixels p = QPainter() p.begin(qimg) w = qimg.width

我有一个矩形PNG,我想处理这个图像,删除一些像素的空白,并保存为PNG格式

这是我最后想要的

正常图像(没有边界-边界半径):

这是我的密码;我尝试了一些事情,但没有正常工作

qimg = QImage(PNG_YOL)
qimg = qimg.convertToFormat(QImage.Format_ARGB32) # making png or will be there black pixels
p = QPainter()
p.begin(qimg)
w = qimg.width() - 1
h=qimg.height() -1
for x in range(int(maxx)):
    dx = 1 # i changed this value to get what i want, it works but not very fine, i am looking better way
    for y in range(int(maxy)):
        if x == 0:
            qimg.setPixel(x, y, Qt.transparent)
            qimg.setPixel(w - x, y, Qt.transparent)
        if x != 0 and y < int(h * 1 / x / dx):
            qimg.setPixel(x, y, Qt.transparent)
            qimg.setPixel(w - x, y, Qt.transparent)
        if x != 0:
            qimg.setPixel(x, int(h * 1 / x / dx), Qt.transparent)
            qimg.setPixel(w - x, int(h * 1 / x / dx), Qt.transparent)

p.end()
qimg.save(PNG_YOL)
qimg=QImage(巴布亚新几内亚)
qimg=qimg.convertToFormat(QImage.Format_ARGB32)#生成png或将有黑色像素
p=QPainter()
p、 开始(qimg)
w=qimg.width()-1
h=qimg.高度()-1
对于范围内的x(int(maxx)):
dx=1#我改变了这个值以得到我想要的,它可以工作但不是很好,我看起来更好
对于范围内的y(int(maxy)):
如果x==0:
qimg.setPixel(x、y、Qt.透明)
qimg.setPixel(w-x,y,Qt.透明)
如果x!=0和y
有了这段代码,我可以得到很好的结果,但我正在寻找更好的方法


注意:我只想在左上角和右上角添加边距。

您可以将QPainter与QPainterPath一起使用,而不是一个像素一个像素地处理:

from PyQt5 import QtCore, QtGui


qin = QtGui.QImage("input.png")

qout = QtGui.QImage(qin.size(), qin.format())
qout.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(qout)
path = QtGui.QPainterPath()

radius = 20
r = qout.rect()
path.arcMoveTo(0, 0, radius, radius, 180)
path.arcTo(0, 0, radius, radius, 180, -90)
path.arcTo(r.width()-radius, 0, radius, radius, 90, -90)
path.lineTo(r.bottomRight())
path.lineTo(r.bottomLeft())
path.closeSubpath()

painter.setClipPath(path)
painter.drawImage(QtCore.QPoint(0, 0), qin)
painter.end()

qout.save("output.png")

非常感谢:)