У меня есть небольшой скрипт для самодельного растрового редактора и мне необходимо реализовать функцию рисования мышью на изображении.
Решение под комментарием вызывает ошибку:
Process finished with exit code -1073740791 (0xC0000409).
Что можно исправить в инициализации изображения или в самой функции рисования для того, чтобы не вызывалась ошибка?
import sys
from PyQt5 import uic
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PIL import Image, ImageDraw
from PIL.ImageQt import ImageQt
class MainWindow(QMainWindow):
""" Блок инициализации """
def __init__(self):
super().__init__()
uic.loadUi('untitled.ui', self)
self.scale = 0
self.openFile()
self.image = Image.open(self.file)
self.a = ImageQt(self.image)
self.pixmap = QPixmap.fromImage(self.a)
self.drawingFlag = True
self.brushColor = Qt.black
self.brushWidth = 1
self.toolButton2Load.clicked.connect(self.openFile)
self.shortcutLoad = QShortcut(QKeySequence("Ctrl+L"), self)
self.shortcutLoad.activated.connect(self.openFile)
self.toolButton1Zoom.clicked.connect(self.scaleImageBigger)
self.toolButton2Zoom.clicked.connect(self.scaleImageSmaller)
self.toolButton3Return.clicked.connect(self.scaleImageReturn)
self.shortcutReturn = QShortcut(QKeySequence("Ctrl+Z"), self)
self.shortcutReturn.activated.connect(self.scaleImageReturn)
self.setMouseTracking(True)
self.initUI()
def initUI(self):
self.imageSizeX = self.image.size[0]
self.startImageSizeX = self.image.size[0]
self.imageSizeY = self.image.size[1]
self.startImageSizeY = self.image.size[1]
self.labelSize.setText(f'{self.imageSizeX}x{self.imageSizeY}')
self.labelImage.resize(self.imageSizeX, self.imageSizeY)
self.labelImage.setPixmap(self.pixmap)
# ╔══════════════════════════════════════════════════════════════════════╗
# ║Блок функций ║
# ╚══════════════════════════════════════════════════════════════════════╝
def openFile(self): # Функция открытия файла
self.file = QFileDialog.getOpenFileName(
self, 'Выбрать картинку', '',
'Картинка (*.jpg);;Картинка (*.png);;Все файлы (*)')[0]
self.pixmap = QPixmap(self.file)
self.image = Image.open(self.file)
self.initUI()
self.pixmap = QPixmap(self.file)
self.image = Image.open(self.file)
self.initUI()
def scaleImageBigger(self): # Функция увеличения изображения
if self.imageSizeX < 5001 and self.imageSizeY < 5001:
self.imageSizeX *= 2
self.imageSizeY *= 2
self.scale += 1
self.pixmap = self.pixmap.scaledToWidth(self.imageSizeX)
self.labelImage.resize(self.imageSizeX, self.imageSizeY)
self.labelImage.move(0, 131)
self.labelSize.setText(f'{self.imageSizeX}x{self.imageSizeY}')
self.labelImage.setPixmap(self.pixmap)
def scaleImageSmaller(self): # Функция уменьшения изображения
if self.imageSizeX // 2 >= self.startImageSizeX and self.imageSizeY // 2 >= self.startImageSizeY:
self.imageSizeX //= 2
self.imageSizeY //= 2
self.scale -= 1
self.pixmap = self.pixmap.scaledToWidth(self.imageSizeX)
self.labelImage.resize(self.imageSizeX, self.imageSizeY)
self.labelImage.move(0, 131)
self.labelSize.setText(f'{self.imageSizeX}x{self.imageSizeY}')
self.labelImage.setPixmap(self.pixmap)
def scaleImageReturn(self): # Восстановление масштаба изображения
self.imageSizeX = self.startImageSizeX
self.imageSizeY = self.startImageSizeY
self.scale = 0
self.pixmap = self.pixmap.scaledToWidth(self.imageSizeX)
self.labelImage.resize(self.imageSizeX, self.imageSizeY)
self.labelImage.move(0, 131)
self.labelSize.setText(f'{self.imageSizeX}x{self.imageSizeY}')
self.labelImage.setPixmap(self.pixmap)
def paintEvent(self, event):
pass
# painter = QPainter(self)
# painter.drawPixmap(self.rect(), self.pixmap)
# ╔══════════════════════════════════════════════════════════════════════╗
# ║ Блок управления ║
# ╚══════════════════════════════════════════════════════════════════════╝
def mousePressEvent(self, event):
pass
# if (event.button() == Qt.LeftButton) and self.drawingFlag:
# self.lastPoint = event.pos()
def mouseMoveEvent(self, event): # Событие движения мыши
coordX = event.x()
coordY = event.y() - 121
if coordY < 0:
coordY = 0
self.labelCoords.setText(f"{coordX}, {coordY}")
# if (event.buttons() & Qt.LeftButton) and self.drawingFlag:
# painter = QPainter(self.pixmap)
# painter.setPen(QPen(self.brushColor, self.brushWidthWidth,
# Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
# painter.drawLine(self.lastPoint, event.pos())
# self.lastPoint = event.pos()
# self.update()
def wheelEvent(self, event): # Событие вращения колеса мыши
numDegrees = event.angleDelta().y() / 8
numSteps = numDegrees // 15
if numSteps > 0:
self.scaleImageBigger()
elif numSteps < 0:
self.scaleImageSmaller()
event.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
drawingFlag
- переключение, которое будет происходить по кнопке и активировать/деактивировать рисование (сейчас всегда включено)
brushColor
и brushWidth
пока нигде не реализованы.
untitled.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1925</width>
<height>1035</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1920</width>
<height>131</height>
</rect>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="tab1File">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</widget>
<widget class="QWidget" name="tab2Main">
<property name="toolTip">
<string><html><head/><body><p>Главная</p></body></html></string>
</property>
<property name="whatsThis">
<string><html><head/><body><p>Главна</p><p><br/></p></body></html></string>
</property>
<property name="styleSheet">
<string notr="true">Главная</string>
</property>
<attribute name="title">
<string>Tab 2</string>
</attribute>
<widget class="QGroupBox" name="groupBox1">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>51</width>
<height>71</height>
</rect>
</property>
<property name="title">
<string/>
</property>
<widget class="QToolButton" name="toolButton1Save">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>51</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>S</string>
</property>
</widget>
<widget class="QToolButton" name="toolButton2Load">
<property name="geometry">
<rect>
<x>0</x>
<y>50</y>
<width>51</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Load</string>
</property>
</widget>
</widget>
<widget class="QLabel" name="label1">
<property name="geometry">
<rect>
<x>10</x>
<y>80</y>
<width>151</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p align="center">Буфер обмена</p></body></html></string>
</property>
</widget>
<widget class="QGroupBox" name="groupBox2">
<property name="geometry">
<rect>
<x>70</x>
<y>10</y>
<width>91</width>
<height>44</height>
</rect>
</property>
<property name="title">
<string/>
</property>
<widget class="QToolButton" name="toolButton1Copy">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>91</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Копировать</string>
</property>
</widget>
<widget class="QToolButton" name="toolButton2Cut">
<property name="geometry">
<rect>
<x>0</x>
<y>21</y>
<width>91</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Вырезать</string>
</property>
</widget>
</widget>
<widget class="Line" name="linev1">
<property name="geometry">
<rect>
<x>170</x>
<y>0</y>
<width>3</width>
<height>101</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<widget class="QGroupBox" name="groupBox3">
<property name="geometry">
<rect>
<x>180</x>
<y>10</y>
<width>51</width>
<height>71</height>
</rect>
</property>
<property name="title">
<string/>
</property>
<widget class="QToolButton" name="toolButton1Select">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>51</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>Sel</string>
</property>
</widget>
<widget class="QComboBox" name="comboBox2SelectForm">
<property name="geometry">
<rect>
<x>1</x>
<y>51</y>
<width>49</width>
<height>20</height>
</rect>
</property>
</widget>
</widget>
<widget class="QLabel" name="label2">
<property name="geometry">
<rect>
<x>180</x>
<y>80</y>
<width>151</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p align="center">Изображение</p></body></html></string>
</property>
</widget>
<widget class="QGroupBox" name="groupBox4">
<property name="geometry">
<rect>
<x>240</x>
<y>10</y>
<width>111</width>
<height>63</height>
</rect>
</property>
<property name="title">
<string/>
</property>
<widget class="QToolButton" name="toolButton1Crop">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>111</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Обрезать</string>
</property>
</widget>
<widget class="QToolButton" name="toolButton2ChangeSize">
<property name="geometry">
<rect>
<x>0</x>
<y>21</y>
<width>111</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Изменить размер</string>
</property>
</widget>
<widget class="QComboBox" name="comboBox3Rotate">
<property name="geometry">
<rect>
<x>1</x>
<y>43</y>
<width>109</width>
<height>20</height>
</rect>
</property>
</widget>
</widget>
<widget class="Line" name="linev2">
<property name="geometry">
<rect>
<x>360</x>
<y>0</y>
<width>3</width>
<height>101</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>370</x>
<y>10</y>
<width>97</width>
<height>61</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="2">
<widget class="QToolButton" name="toolButton3Text">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="toolButton2Fill">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QToolButton" name="toolButton1Pencil">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QToolButton" name="toolButton4Erase">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QToolButton" name="toolButton5Pipet">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QToolButton" name="toolButton6Scale">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QLabel" name="label3">
<property name="geometry">
<rect>
<x>360</x>
<y>80</y>
<width>121</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p align="center">Инструменты</p></body></html></string>
</property>
</widget>
<widget class="Line" name="linev3">
<property name="geometry">
<rect>
<x>480</x>
<y>0</y>
<width>3</width>
<height>101</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<widget class="QToolButton" name="toolButton3Return">
<property name="geometry">
<rect>
<x>590</x>
<y>10</y>
<width>51</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>100%</string>
</property>
</widget>
<widget class="QToolButton" name="toolButton1Zoom">
<property name="geometry">
<rect>
<x>490</x>
<y>10</y>
<width>51</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>+</string>
</property>
</widget>
<widget class="QToolButton" name="toolButton2Zoom">
<property name="geometry">
<rect>
<x>540</x>
<y>10</y>
<width>51</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>-</string>
</property>
</widget>
<widget class="QLabel" name="label4">
<property name="geometry">
<rect>
<x>500</x>
<y>80</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p align="center">Масштаб</p></body></html></string>
</property>
</widget>
<widget class="Line" name="linev4">
<property name="geometry">
<rect>
<x>650</x>
<y>0</y>
<width>3</width>
<height>101</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<widget class="QLabel" name="labelSelectSize">
<property name="geometry">
<rect>
<x>650</x>
<y>30</y>
<width>371</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLabel" name="labelSize">
<property name="geometry">
<rect>
<x>650</x>
<y>50</y>
<width>371</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLabel" name="label6">
<property name="geometry">
<rect>
<x>650</x>
<y>80</y>
<width>371</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p align="center">Информация</p></body></html></string>
</property>
</widget>
<widget class="QLabel" name="labelCoords">
<property name="geometry">
<rect>
<x>650</x>
<y>10</y>
<width>371</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="Line" name="linev5">
<property name="geometry">
<rect>
<x>1020</x>
<y>0</y>
<width>3</width>
<height>101</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<widget class="QToolButton" name="toolButton4Paint">
<property name="geometry">
<rect>
<x>1030</x>
<y>10</y>
<width>51</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>Pain</string>
</property>
</widget>
<widget class="QSpinBox" name="spinBoxWight">
<property name="geometry">
<rect>
<x>1090</x>
<y>10</y>
<width>121</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButtonColorChose">
<property name="geometry">
<rect>
<x>1090</x>
<y>40</y>
<width>121</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>ChoseColor</string>
</property>
</widget>
<widget class="QLabel" name="label7">
<property name="geometry">
<rect>
<x>1020</x>
<y>80</y>
<width>201</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p align="center">Кисть</p></body></html></string>
</property>
</widget>
<widget class="Line" name="linev7">
<property name="geometry">
<rect>
<x>1220</x>
<y>0</y>
<width>3</width>
<height>101</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<widget class="QLabel" name="label8">
<property name="geometry">
<rect>
<x>1220</x>
<y>80</y>
<width>231</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p align="center">Фигуры</p></body></html></string>
</property>
</widget>
<widget class="QComboBox" name="comboBox">
<property name="geometry">
<rect>
<x>1230</x>
<y>10</y>
<width>111</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QGroupBox" name="groupBox5">
<property name="geometry">
<rect>
<x>1350</x>
<y>10</y>
<width>91</width>
<height>42</height>
</rect>
</property>
<property name="title">
<string/>
</property>
<widget class="QToolButton" name="toolButton1ShapeOutline">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>91</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Контур</string>
</property>
</widget>
<widget class="QToolButton" name="toolButton2ShapeFill">
<property name="geometry">
<rect>
<x>0</x>
<y>20</y>
<width>91</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>Заливка</string>
</property>
</widget>
</widget>
<widget class="Line" name="linev8">
<property name="geometry">
<rect>
<x>1450</x>
<y>0</y>
<width>3</width>
<height>101</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</widget>
</widget>
<widget class="Line" name="lineh1">
<property name="geometry">
<rect>
<x>0</x>
<y>24</y>
<width>1920</width>
<height>3</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="Line" name="lineh2">
<property name="geometry">
<rect>
<x>0</x>
<y>128</y>
<width>1921</width>
<height>3</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QLabel" name="labelImage">
<property name="geometry">
<rect>
<x>0</x>
<y>131</y>
<width>10</width>
<height>10</height>
</rect>
</property>
<property name="text">
<string>Image</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1925</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>````