Reputation: 57
i'm struggling to set pixels to a specific RGBA value with a numpy array.
Im using the QImage.Format_RGBA8888
.
When i'm doing it with QImage.Format_RGB888
it makes what i want, but i'd like to have transparency.
I'm not sure What i'm overlooking?
Thanks for any advice :)
import sys
from PyQt5 import QtWidgets as qw
from PyQt5 import QtGui as qg
from PyQt5 import QtCore as qc
import numpy as np
class MainWindow(qw.QMainWindow):
def __init__(self):
super(qw.QMainWindow, self).__init__()
self.height = 500
self.width = 500
self.setWindowTitle("V.0.0")
self.setMinimumSize(self.width, self.height)
# display
self.display = qw.QLabel()
self.setCentralWidget(self.display)
# self.display.setGeometry(qc.QRect(0, 0, self.width, self.height))
self.background()
self.display.setPixmap(qg.QPixmap.fromImage(self.world_img))
self.show()
def background(self):
self.world = np.zeros([self.width, self.height, 4], dtype=np.uint8)
self.world += 255 # should do all black?
self.world[20, 30, :] = [135, 23, 53, 1]
self.world[21, 31, :] = [135, 23, 53, 1]
self.world[22, 32, :] = [135, 23, 53, 1]
self.world_img = qg.QImage(self.world, self.width, self.height, qg.QImage.Format_RGBA8888)
# Start app
if __name__ == '__main__':
app = qw.QApplication(sys.argv)
Game = MainWindow()
sys.exit(app.exec_())
Upvotes: 1
Views: 654
Reputation: 600
You have the right idea!
The thing you're missing is that RGB values of 255,255,255 is white, not black. As well, the transparency "A" axis goes from 0 - 255, where 0 is opaque and 255 is completely transparent.
Examples:
Small thing for visibility, consider setting a test patch rather than a test pixel, like so:
self.world[20:40, 30:50, :] = [135, 23, 53, 128]
Upvotes: 3