【问题标题】:How to move a pixel column in PyQt5 Pixmap?如何在 PyQt5 Pixmap 中移动像素列?
【发布时间】:2021-05-20 16:10:48
【问题描述】:

我正在制作一个 python 项目,我正在使用 pyqt。目的是显示一张图片(我正在使用 Pixmap),我希望这张图片的任何像素列根据时间随机下降。目的是创造一种“溶解”屏幕的效果。

这是我的代码:

#-*- 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()

【问题讨论】:

  • 你说的按时间随机下去是什么意思?你能放一张你想要得到的图片(也许是 gif)吗?
  • 我想要这样的i.stack.imgur.com/OppSt.png
  • 那张图片无法帮助理解,也许几张图片会有所帮助,例如放置原始图片并显示其他图片,其中该过程已应用于列
  • 我认为一个名为 Twith Booster 的旧病毒显示了我想要的效果。我想用 pyqt 在屏幕上产生同样的效果。这是病毒在后台屏幕上所做的链接youtube.com/watch?v=RW2u3adF2yI 这里你可以看到屏幕正在“融化”

标签: python pyqt pixmap


【解决方案1】:

一种可能的解决方案是复制和粘贴矩形,但具有垂直距离:

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()

【讨论】:

  • 非常感谢,这通常是我想要的 :) 我会继续努力并回来 :)
猜你喜欢
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多