class Drawer(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setAttribute(Qt.WA_StaticContents) self.h = 720 self.w = 1280 self.Font_Color = Qt.white self.myPenWidth = 15 self.myPenColor = Qt.black self.image = QImage(self.w, self.h, QImage.Format_RGB32) self.path = QPainterPath() self.clearImage() self.flag = False self.colors = [[100, 0, 0], [150, 0, 0], [200, 0, 0], [0, 100, 0], [0, 150, 0], [0, 200, 0], [0, 0, 100], [0, 0, 150], [0, 0, 200]] self.color = 0 def setPenColor(self, newColor): self.path = QPainterPath() self.myPenColor = newColor def setPenWidth(self): i, okBtnPressed = QInputDialog.getInt(self, "Размер кисти", "Введите размер кисти", self.myPenWidth, 1, 100, 1) if okBtnPressed: self.path = QPainterPath() self.myPenWidth = i self.myCursor() def setFont(self): color = QColorDialog.getColor() self.Font_Color = color self.clearImage() def clearImage(self): self.path = QPainterPath() self.image.fill(self.Font_Color) self.update() def saveImage(self): i, okBtnPressed = QInputDialog.getText(self, "Название файла", "Введите название файла") if okBtnPressed: name = i if "." in name: self.image.save(name, name.split(".")[1].upper()) else: i1, okBtnPressed1 = QInputDialog.getItem( self, "Расширение", "Выберите расширение", ("jpg", "png")) self.image.save(f"{name}.{i1}", i1.upper()) def loadImage(self): i, okBtnPressed = QInputDialog.getText(self, "Название файла", "Введите название файла") if okBtnPressed: name = i if "." in name: self.image = QImage(name, name.split(".")[1].upper()) else: i1, okBtnPressed1 = QInputDialog.getItem( self, "Расширение", "Выберите расширение", ("jpg", "png"), 1, False) self.image = QImage(f"{name}.{i1}", i1.upper()) def paintEvent(self, event): painter = QPainter(self) painter.drawImage(event.rect(), self.image, self.rect()) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.flag = True self.path.moveTo(event.pos()) def mouseMoveEvent(self, event): if self.flag: self.path.lineTo(event.pos()) p = QPainter(self.image) p.setPen( QPen(self.myPenColor, self.myPenWidth, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) p.drawPath(self.path) p.end() self.update() else: self.path = QPainterPath() def sizeHint(self): if self.w == 1280 and self.h == 720: return QSize(1280, 720) return QSize(640, 480) self.update() def ChangeRes(self): if self.w == 1280 and self.h == 720: self.w = 640 self.h = 480 else: self.w = 1280 self.h = 720 self.update() self.image = QImage(self.w, self.h, QImage.Format_RGB32) self.path = QPainterPath() self.clearImage() def myCursor(self): cmap = QPixmap("cursor.png") cmap = cmap.scaled(self.myPenWidth + 50 * (1 + self.myPenWidth // 100), self.myPenWidth + 50 * (1 + self.myPenWidth // 100)) color = self.myPenColor self.setCursor(QCursor(cmap)) def run(self): color = QColorDialog.getColor() if color.isValid(): self.setPenColor(color) self.path = QPainterPath() def filter_invert_color(self): x, y = self.image.width(), self.image.height() im2 = Image.new("RGB", (x, y), (0, 0, 0)) pixels2 = im2.load() for i in range(x): for j in range(y): r, g, b = self.image.pixelColor(i, j).getRgb()[:-1] pixels2[i, j] = 255 - r, 255 - g, 255 - b self.image = ImageQt(im2) self.update() self.path = QPainterPath() def filter_black_and_white(self): x, y = self.image.width(), self.image.height() im2 = Image.new("RGB", (x, y), (0, 0, 0)) pixels2 = im2.load() for i in range(x): for j in range(y): r, g, b = self.image.pixelColor(i, j).getRgb()[:-1] bw = (r + g + b) // 3 pixels2[i, j] = bw, bw, bw self.image = ImageQt(im2) self.update() self.path = QPainterPath() def filter_(self): x, y = self.image.width(), self.image.height() im2 = Image.new("RGB", (x, y), (0, 0, 0)) pixels2 = im2.load() for i in range(x): for j in range(y): r, g, b = self.image.pixelColor(i, j).getRgb()[:-1] bw = (r + g + b) // 3 pixels2[i, j] = bw, bw, bw self.image = ImageQt(im2) self.update() self.path = QPainterPath()
class Drawer(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.setAttribute(Qt.WA_StaticContents) self.h = 720 self.w = 1280 self.Font_Color = Qt.white self.myPenWidth = 15 self.myPenColor = Qt.black self.image = QImage(self.w, self.h, QImage.Format_RGB32) self.path = QPainterPath() self.clearImage() def setPenColor(self, newColor): self.path = QPainterPath() self.myPenColor = newColor def setPenWidth(self): i, okBtnPressed = QInputDialog.getInt(self, "Размер кисти", "Введите размер кисти", self.myPenWidth, 1, 100, 1) if okBtnPressed: self.path = QPainterPath() self.myPenWidth = i self.myCursor() def setFont(self): color = QColorDialog.getColor() self.Font_Color = color self.clearImage() def clearImage(self): self.path = QPainterPath() self.image.fill(self.Font_Color) self.update() def saveImage(self): try: fname = QFileDialog.getSaveFileName(self, 'Выбор файла', '.')[0] self.image.save(fname, fname.split(".")[1].upper()) except Exception: pass def loadImage(self): self.path = QPainterPath() try: fname = QFileDialog.getOpenFileName(self, 'Выбор файла', '.')[0] self.image = QImage(fname, fname.split(".")[1].upper()) except Exception: pass def paintEvent(self, event): painter = QPainter(self) painter.drawImage(event.rect(), self.image, self.rect()) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.flag = True self.path.moveTo(event.pos()) def mouseMoveEvent(self, event): if self.flag: self.path.lineTo(event.pos()) p = QPainter(self.image) p.setPen( QPen(self.myPenColor, self.myPenWidth, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) p.drawPath(self.path) p.end() self.update() else: self.path = QPainterPath() def sizeHint(self): if self.w == 1280 and self.h == 720: return QSize(1280, 720) return QSize(640, 480) self.update() def ChangeRes(self): if self.w == 1280 and self.h == 720: self.w = 640 self.h = 480 else: self.w = 1280 self.h = 720 self.update() self.image = QImage(self.w, self.h, QImage.Format_RGB32) self.path = QPainterPath() self.clearImage() def myCursor(self): cmap = QPixmap("cursor.png") cmap = cmap.scaled(self.myPenWidth + 50 * (1 + self.myPenWidth // 100), self.myPenWidth + 50 * (1 + self.myPenWidth // 100)) color = self.myPenColor self.setCursor(QCursor(cmap)) def run(self): color = QColorDialog.getColor() if color.isValid(): self.setPenColor(color) self.path = QPainterPath() def filter_invert_color(self): x, y = self.image.width(), self.image.height() im2 = Image.new("RGB", (x, y), (0, 0, 0)) pixels2 = im2.load() for i in range(x): for j in range(y): r, g, b = self.image.pixelColor(i, j).getRgb()[:-1] pixels2[i, j] = 255 - r, 255 - g, 255 - b self.image = ImageQt(im2) self.update() self.path = QPainterPath() def filter_black_and_white(self): x, y = self.image.width(), self.image.height() im2 = Image.new("RGB", (x, y), (0, 0, 0)) pixels2 = im2.load() for i in range(x): for j in range(y): r, g, b = self.image.pixelColor(i, j).getRgb()[:-1] bw = (r + g + b) // 3 pixels2[i, j] = bw, bw, bw self.image = ImageQt(im2) self.update() self.path = QPainterPath() def filter_left(self): x, y = self.image.width(), self.image.height() im2 = Image.new("RGB", (x, y), (0, 0, 0)) pixels2 = im2.load() for i in range(x): for j in range(y): r, g, b = self.image.pixelColor(i, j).getRgb()[:-1] pixels2[i, j] = r, g, b im2 = im2.transpose(Image.ROTATE_90) self.image = ImageQt(im2) self.update() def filter_right(self): x, y = self.image.width(), self.image.height() im2 = Image.new("RGB", (x, y), (0, 0, 0)) pixels2 = im2.load() for i in range(x): for j in range(y): r, g, b = self.image.pixelColor(i, j).getRgb()[:-1] pixels2[i, j] = r, g, b im2 = im2.transpose(Image.ROTATE_270) self.image = ImageQt(im2) self.update()
class PanelDraw(QWidget, Ui_PanelDraw): def __init__(self, Parent): QWidget.__init__(self) self.setupUi(self) self.Image = QImage(self.size(), QImage.Format_RGB32) self.Image.fill(Qt.white) self.dx = Parent.widget.x() / 2 self.dy = Parent.widget.y() / 2 self.readOnly = False self.drawing = False self.penSize = 2 self.penColor = Qt.black pass def mousePressEvent(self, event): if (event.button() == Qt.LeftButton) & (not self.readOnly): self.drawing = True self.lastPoint = event.pos() self.lastPoint.setX(self.lastPoint.x() + self.dx) self.lastPoint.setY(self.lastPoint.y() + self.dy) pass def mouseMoveEvent(self, event): if (event.buttons() & Qt.LeftButton) & self.drawing: painter = QPainter(self.Image) painter.setPen( QPen(self.penColor, self.penSize, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) pos = event.pos() pos.setX(pos.x() + self.dx) pos.setY(pos.y() + self.dy) painter.drawLine(self.lastPoint, pos) self.lastPoint = pos self.update() pass def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: self.drawing = False pass def paintEvent(self, event): canvas = QPainter(self) canvas.drawImage(self.rect(), self.Image, self.Image.rect()) pass def DrawImage(self, image): self.Image = ImageQt(image) self.update() pass def Clear(self): self.Image.fill(Qt.white) self.update() pass