通过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()
答案 0 :(得分:0)
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()