Solar
Solar

Reputation: 3

How to move a pixel column in PyQt5 Pixmap?

I am making a python project,I am using pyqt. The aim is to show a picture (I am using Pixmap) and I want that any column of pixel of this picture go down randomly according to the time. The aim is to create an effect of 'dissolve' the screen.

Here is my code :

#-*- coding: utf-8 -*-

# ---------- Imports ----------

import sys, time, os, random
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

# ---------- Main ----------

class Ui (QMainWindow):

    def __init__(self):
        QWidget.__init__(self)

        # ----- Fichiers -----

        folder_path = os.path.dirname(os.path.abspath(__file__))
        self.picture_path = str(folder_path + '\\solar_wallpaper.jpg')
        self.icon_path = str(folder_path + '\\solar_icon.ico')

        # ----- Configuration de la fenêtre -----

        self.setWindowFlags(self.windowFlags() &~ Qt.WindowCloseButtonHint)
        self.setWindowFlags(self.windowFlags() &~ Qt.WindowMinimizeButtonHint)
        self.setWindowFlags(self.windowFlags() &~ Qt.WindowMaximizeButtonHint)
        self.setWindowTitle('Solar')
        self.setWindowIcon(QIcon(self.icon_path))
        self.setStyleSheet("background-color: black;")

        # ----- Appel des méthodes -----

        self.init_components()
        self.init_layout()
        self.showFullScreen()

    def init_components (self):

        self.setCentralWidget(QGroupBox())
        self.picture = QLabel(self)
        self.picture.setScaledContents(True)
        self.picture.setPixmap(QPixmap(self.picture_path))
        self.picture.setAlignment(Qt.AlignCenter)
        colonne = self.picture.width()
        for i in range (colonne):
            None

    def init_layout (self):

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.picture)
        self.centralWidget().setLayout(self.layout)

# ---------- Launcher ----------

app = QApplication.instance()
if not app :
    app = QApplication(sys.argv)
ui = Ui()
app.exec()

Upvotes: 0

Views: 336

Answers (1)

eyllanesc
eyllanesc

Reputation: 243965

A possible solution is to copy and paste pieces of rectangles but with a vertical distance:

import random
import sys

from PyQt5.QtCore import QRect, QTimer
from PyQt5.QtGui import QColor, QPainter, QPixmap
from PyQt5.QtWidgets import QApplication, QLabel

COLUMN_WIDTH = 10
DELTA = 10


def build_pixmap():
    delta = 20
    pixmap = QPixmap(512, 512)
    pixmap.fill(QColor("blue"))
    painter = QPainter(pixmap)
    for i in range(5, 15):
        dt = i * delta
        r = QRect(pixmap.rect().adjusted(0, dt, 0, -dt))
        color = QColor(*random.sample(range(255), 3))
        painter.fillRect(r, color)
    painter.end()
    return pixmap


def aply_effect(pixmap, number_of_columns):
    i = round(pixmap.width() / COLUMN_WIDTH)
    for _ in range(number_of_columns):
        j = random.randint(0, i)
        rect = QRect(j * COLUMN_WIDTH, 0, COLUMN_WIDTH, pixmap.height())

        pix = pixmap.copy(rect)
        painter = QPainter(pixmap)
        painter.drawPixmap(rect.translated(0, DELTA), pix)
    painter.end()
    return pixmap


def main():
    app = QApplication(sys.argv)

    pixmap_demo = build_pixmap()
    label = QLabel(pixmap=pixmap_demo)
    label.show()

    def on_timeout():
        out_pixmap = aply_effect(label.pixmap(), 40)
        label.setPixmap(out_pixmap)

    timer = QTimer(interval=40, timeout=on_timeout)
    timer.start()

    app.exec_()


if __name__ == "__main__":
    main()

Upvotes: 1

Related Questions