更改颜色 PNG 图像 QPushbutton
change color PNG Image QPushbutton
在 QtWidgets.QColorDialog 设置颜色后,我尝试更改 PNG 图像的颜色。 Python
---> 至
然而,当我应用我的代码时,结果是:
它改变了我的 PNG 图片的背景
png 源在这里:DMX_Led
我写了一小部分代码:
import os
import sys
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
dir_path = os.path.dirname(os.path.realpath(__file__))
class Ui_MainWindows(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindows,self).__init__()
self.central_widget = QtWidgets.QWidget()
self.setCentralWidget(self.central_widget)
self.setWindowTitle("Change color PNG Test")
self.setStyleSheet("background-color:black;")
self.resize(350, 350)
self.DMX_Color = QtGui.QColor('#000000')
self.button_DMX = QtWidgets.QPushButton(self.central_widget)
print("Add ")
path_image = os.path.join(dir_path,"Ressource/Rampe3.png").replace("\", "/")
qss = 'border-image: url({})'.format(path_image)
self.button_DMX.setGeometry(100, 100, 100, 61)
self.button_DMX.setStyleSheet(qss)
self.button_DMX.clicked.connect(self.processButton_DMX)
def processButton_DMX(self):
print("Projector DMX clicked ")
self.DMX_Color.setRed(0)
self.DMX_Color.setGreen(0)
self.DMX_Color.setBlue(255)
print("Change Color Button DMX")
effect = QtWidgets.QGraphicsColorizeEffect(self.button_DMX)
effect.setColor(self.DMX_Color)
self.button_DMX.setGraphicsEffect(effect)
def main():
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_MainWindows()
MainWindow.show()
rc = app.exec_()
sys.exit(rc)
if __name__ == "__main__":
main()
QGraphicsEffect
应用于透明像素,因此它不是最佳选择,一个可能的解决方案是使用 QImage
并通过将像素应用于像素颜色将其设置为图标变化:
import os
import sys
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
dir_path = os.path.dirname(os.path.realpath(__file__))
class Ui_MainWindows(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindows,self).__init__()
self.central_widget = QtWidgets.QWidget()
self.setCentralWidget(self.central_widget)
self.setWindowTitle("Change color PNG Test")
self.setStyleSheet("background-color:black;")
self.resize(350, 350)
self.DMX_Color = QtGui.QColor('#000000')
self.button_DMX = QtWidgets.QPushButton(self.central_widget)
self.button_DMX.move(100, 100)
path_image = os.path.join(dir_path, "Ressource/Rampe3.png").replace("\", "/")
self.image = QtGui.QImage(path_image)
self.button_DMX.clicked.connect(self.processButton_DMX)
self.change_image()
def change_image(self, color=QtGui.QColor("white")):
for x in range(self.image.width()):
for y in range(self.image.height()):
pcolor = self.image.pixelColor(x, y)
if pcolor.alpha() > 0:
n_color = QtGui.QColor(color)
n_color.setAlpha(pcolor.alpha())
self.image.setPixelColor(x, y, n_color)
self.button_DMX.setIcon(QtGui.QIcon(QtGui.QPixmap.fromImage(self.image)))
self.button_DMX.setIconSize(self.image.size())
self.button_DMX.setFixedSize(self.image.size())
@QtCore.pyqtSlot()
def processButton_DMX(self):
color = QtWidgets.QColorDialog.getColor(QtCore.Qt.white, self)
if color.isValid():
self.change_image(color)
def main():
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_MainWindows()
MainWindow.show()
rc = app.exec_()
sys.exit(rc)
if __name__ == "__main__":
main()
在 QtWidgets.QColorDialog 设置颜色后,我尝试更改 PNG 图像的颜色。 Python
---> 至
然而,当我应用我的代码时,结果是:
它改变了我的 PNG 图片的背景
png 源在这里:DMX_Led
我写了一小部分代码:
import os
import sys
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
dir_path = os.path.dirname(os.path.realpath(__file__))
class Ui_MainWindows(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindows,self).__init__()
self.central_widget = QtWidgets.QWidget()
self.setCentralWidget(self.central_widget)
self.setWindowTitle("Change color PNG Test")
self.setStyleSheet("background-color:black;")
self.resize(350, 350)
self.DMX_Color = QtGui.QColor('#000000')
self.button_DMX = QtWidgets.QPushButton(self.central_widget)
print("Add ")
path_image = os.path.join(dir_path,"Ressource/Rampe3.png").replace("\", "/")
qss = 'border-image: url({})'.format(path_image)
self.button_DMX.setGeometry(100, 100, 100, 61)
self.button_DMX.setStyleSheet(qss)
self.button_DMX.clicked.connect(self.processButton_DMX)
def processButton_DMX(self):
print("Projector DMX clicked ")
self.DMX_Color.setRed(0)
self.DMX_Color.setGreen(0)
self.DMX_Color.setBlue(255)
print("Change Color Button DMX")
effect = QtWidgets.QGraphicsColorizeEffect(self.button_DMX)
effect.setColor(self.DMX_Color)
self.button_DMX.setGraphicsEffect(effect)
def main():
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_MainWindows()
MainWindow.show()
rc = app.exec_()
sys.exit(rc)
if __name__ == "__main__":
main()
QGraphicsEffect
应用于透明像素,因此它不是最佳选择,一个可能的解决方案是使用 QImage
并通过将像素应用于像素颜色将其设置为图标变化:
import os
import sys
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
dir_path = os.path.dirname(os.path.realpath(__file__))
class Ui_MainWindows(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindows,self).__init__()
self.central_widget = QtWidgets.QWidget()
self.setCentralWidget(self.central_widget)
self.setWindowTitle("Change color PNG Test")
self.setStyleSheet("background-color:black;")
self.resize(350, 350)
self.DMX_Color = QtGui.QColor('#000000')
self.button_DMX = QtWidgets.QPushButton(self.central_widget)
self.button_DMX.move(100, 100)
path_image = os.path.join(dir_path, "Ressource/Rampe3.png").replace("\", "/")
self.image = QtGui.QImage(path_image)
self.button_DMX.clicked.connect(self.processButton_DMX)
self.change_image()
def change_image(self, color=QtGui.QColor("white")):
for x in range(self.image.width()):
for y in range(self.image.height()):
pcolor = self.image.pixelColor(x, y)
if pcolor.alpha() > 0:
n_color = QtGui.QColor(color)
n_color.setAlpha(pcolor.alpha())
self.image.setPixelColor(x, y, n_color)
self.button_DMX.setIcon(QtGui.QIcon(QtGui.QPixmap.fromImage(self.image)))
self.button_DMX.setIconSize(self.image.size())
self.button_DMX.setFixedSize(self.image.size())
@QtCore.pyqtSlot()
def processButton_DMX(self):
color = QtWidgets.QColorDialog.getColor(QtCore.Qt.white, self)
if color.isValid():
self.change_image(color)
def main():
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_MainWindows()
MainWindow.show()
rc = app.exec_()
sys.exit(rc)
if __name__ == "__main__":
main()