Guest User

Untitled

a guest
Nov 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. from PyQt5.QtWidgets import *
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtCore import *
  4.  
  5.  
  6. IMG_FILE_NAME = 'img.png'
  7.  
  8.  
  9. class Widget(QWidget):
  10. def __init__(self):
  11. super().__init__()
  12.  
  13. self.setWindowTitle('qt_pixel_draw_image')
  14.  
  15. self.img = QImage(IMG_FILE_NAME)
  16. self.img_width = self.img.size().width()
  17. self.img_height = self.img.size().height()
  18.  
  19. # Сгенерируем список координат пикселей
  20. self.pixel_list = [(y, x) for y in range(self.img_height) for x in range(self.img_width)]
  21.  
  22. # Перемешаем элементы списка случайным образом
  23. import random
  24. random.shuffle(self.pixel_list)
  25.  
  26. self.new_img = QImage(self.img_width, self.img_height, QImage.Format_RGB32)
  27. self.new_img.fill(Qt.white)
  28.  
  29. self.timer = QTimer()
  30. self.timer.timeout.connect(self._draw_pixel)
  31. self.timer.start(1) # 1 ms
  32.  
  33. def _draw_pixel(self):
  34. # Если список пустой
  35. if not self.pixel_list:
  36. self.timer.stop()
  37. return
  38.  
  39. y, x = self.pixel_list.pop()
  40. pixel = self.img.pixel(x, y)
  41.  
  42. # Установка пикселя в новой картинке
  43. self.new_img.setPixel(x, y, pixel)
  44.  
  45. # Перерисование виджета
  46. self.update()
  47.  
  48. def paintEvent(self, event):
  49. painter = QPainter(self)
  50.  
  51. # Рисуем старую картинку
  52. painter.drawImage(0, 0, self.img)
  53.  
  54. # Рисуем новую картинку
  55. painter.drawImage(0 + self.img.width() + 10, 0, self.new_img)
  56.  
  57.  
  58. if __name__ == '__main__':
  59. app = QApplication([])
  60.  
  61. w = Widget()
  62. w.resize(200, 100)
  63. w.show()
  64.  
  65. app.exec()
Add Comment
Please, Sign In to add comment