pyqt5で画像全体を徐々に透明化させたいです。下は試しに書いてみたコードです。
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtGui,QtCore, QtWidgets
import os.path
from PIL import Image, ImageOps, ImageFilter
from PIL.ImageQt import ImageQt
class Example(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.button1 = QtWidgets.QPushButton('実行',self)
self.button1.clicked.connect(self.calc)
self.count=255
self.button2 = QtWidgets.QPushButton(self)
global img
img = "./image/star.png"
self.button2.setIcon(QIcon(img))
self.button2.setIconSize(QSize(400,300))
layout=QtWidgets.QVBoxLayout()
layout.addWidget(self.button1)
layout.addWidget(self.button2)
self.setLayout(layout)
def calc(self):
image = Image.open(img)
image=image.copy()
self.count=self.count-10
if self.count<0:
self.count=0
else:
pass
image.putalpha(self.count)#0~255まで
qim = ImageQt(image)
qim2=QPixmap.fromImage(qim)
self.button2.setIcon(QIcon(qim2))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
これを実行すると次のようなGUIが作成されます。
星の画像はパワーポイントで保存した図形で、背景が透明になっています。ここで、実行ボタンを押すと、その度に星の画像が徐々に透明化されていくようにコードを書いたつもりです。しかし、実際に実行ボタンを押すと、次のように星の背景が黒い画像となりました。
何故こんなことが起こってしまうのでしょうか。