簡體   English   中英

PyQt QImage Border Radius with for loop

[英]PyQt QImage Border Radius with for loop

我有 Rectangle PNG,我想處理這個圖像,刪除一些像素到邊緣並將其保存為 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)

使用此代碼,我可以獲得很好的結果,但我正在尋找更好的方法。

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

您可以將 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM