Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtWidgets import *
- from PyQt5.QtGui import *
- from PyQt5.QtCore import *
- IMG_FILE_NAME = 'img.png'
- class Widget(QWidget):
- def __init__(self):
- super().__init__()
- self.setWindowTitle('qt_pixel_draw_image')
- self.img = QImage(IMG_FILE_NAME)
- self.img_width = self.img.size().width()
- self.img_height = self.img.size().height()
- # Сгенерируем список координат пикселей
- self.pixel_list = [(y, x) for y in range(self.img_height) for x in range(self.img_width)]
- # Перемешаем элементы списка случайным образом
- import random
- random.shuffle(self.pixel_list)
- self.new_img = QImage(self.img_width, self.img_height, QImage.Format_RGB32)
- self.new_img.fill(Qt.white)
- self.timer = QTimer()
- self.timer.timeout.connect(self._draw_pixel)
- self.timer.start(1) # 1 ms
- def _draw_pixel(self):
- # Если список пустой
- if not self.pixel_list:
- self.timer.stop()
- return
- y, x = self.pixel_list.pop()
- pixel = self.img.pixel(x, y)
- # Установка пикселя в новой картинке
- self.new_img.setPixel(x, y, pixel)
- # Перерисование виджета
- self.update()
- def paintEvent(self, event):
- painter = QPainter(self)
- # Рисуем старую картинку
- painter.drawImage(0, 0, self.img)
- # Рисуем новую картинку
- painter.drawImage(0 + self.img.width() + 10, 0, self.new_img)
- if __name__ == '__main__':
- app = QApplication([])
- w = Widget()
- w.resize(200, 100)
- w.show()
- app.exec()
Add Comment
Please, Sign In to add comment