コード例 #1
0
    def prepare_krita_file(self):
        wBase = max(
            self.widthUnit.pixelsForUnit(self.spn_width.value(),
                                         self.DPI.value()), 1)
        bL = self.bleedLeftUnit.pixelsForUnit(self.bleedLeft.value(),
                                              self.DPI.value())
        bR = self.bleedRightUnit.pixelsForUnit(self.bleedRight.value(),
                                               self.DPI.value())
        mL = self.marginLeftUnit.pixelsForUnit(self.marginLeft.value(),
                                               self.DPI.value())
        mR = self.marginRightUnit.pixelsForUnit(self.marginRight.value(),
                                                self.DPI.value())

        hBase = max(
            self.heightUnit.pixelsForUnit(self.spn_height.value(),
                                          self.DPI.value()), 1)
        bT = self.bleedTopUnit.pixelsForUnit(self.bleedTop.value(),
                                             self.DPI.value())
        bB = self.bleedBottomUnit.pixelsForUnit(self.bleedBottom.value(),
                                                self.DPI.value())
        mT = self.marginTopUnit.pixelsForUnit(self.marginTop.value(),
                                              self.DPI.value())
        mB = self.marginBottomUnit.pixelsForUnit(self.marginBottom.value(),
                                                 self.DPI.value())

        template = Application.createDocument((wBase + bL + bR),
                                              (hBase + bT + bB),
                                              self.templateName.text(), "RGBA",
                                              "U8", "sRGB built-in",
                                              self.DPI.value())

        backgroundNode = template.activeNode()
        backgroundNode.setName(i18n("Background"))
        pixelByteArray = QByteArray()
        pixelByteArray = backgroundNode.pixelData(0, 0, (wBase + bL + bR),
                                                  (hBase + bT + bB))
        white = int(255)
        pixelByteArray.fill(white.to_bytes(1, byteorder='little'))
        backgroundNode.setPixelData(pixelByteArray, 0, 0, (wBase + bL + bR),
                                    (hBase + bT + bB))
        backgroundNode.setLocked(True)

        sketchNode = template.createNode(i18n("Sketch"), "paintlayer")
        template.rootNode().setChildNodes([backgroundNode, sketchNode])

        verticalGuides = []
        verticalGuides.append(bL)
        verticalGuides.append(bL + mL)
        verticalGuides.append((bL + wBase) - mR)
        verticalGuides.append(bL + wBase)

        horizontalGuides = []
        horizontalGuides.append(bT)
        horizontalGuides.append(bT + mT)
        horizontalGuides.append((bT + hBase) - mB)
        horizontalGuides.append(bT + hBase)

        template.setHorizontalGuides(horizontalGuides)
        template.setVerticalGuides(verticalGuides)
        template.setGuidesVisible(True)
        template.setGuidesLocked(True)

        self.urlSavedTemplate = os.path.join(self.templateDirectory,
                                             self.templateName.text() + ".kra")
        success = template.exportImage(self.urlSavedTemplate, InfoObject())
        print("CPMT: Template", self.templateName.text(), "made and saved.")
        template.waitForDone()
        template.close()

        return success
コード例 #2
0
    def prepare_krita_file(self):
        wBase = max(
            self.widthUnit.pixelsForUnit(self.spn_width.value(),
                                         self.DPI.value()), 1)
        bL = self.bleedLeftUnit.pixelsForUnit(self.bleedLeft.value(),
                                              self.DPI.value())
        bR = self.bleedRightUnit.pixelsForUnit(self.bleedRight.value(),
                                               self.DPI.value())
        mL = self.marginLeftUnit.pixelsForUnit(self.marginLeft.value(),
                                               self.DPI.value())
        mR = self.marginRightUnit.pixelsForUnit(self.marginRight.value(),
                                                self.DPI.value())

        hBase = max(
            self.heightUnit.pixelsForUnit(self.spn_height.value(),
                                          self.DPI.value()), 1)
        bT = self.bleedTopUnit.pixelsForUnit(self.bleedTop.value(),
                                             self.DPI.value())
        bB = self.bleedBottomUnit.pixelsForUnit(self.bleedBottom.value(),
                                                self.DPI.value())
        mT = self.marginTopUnit.pixelsForUnit(self.marginTop.value(),
                                              self.DPI.value())
        mB = self.marginBottomUnit.pixelsForUnit(self.marginBottom.value(),
                                                 self.DPI.value())

        template = Application.createDocument((wBase + bL + bR),
                                              (hBase + bT + bB),
                                              self.templateName.text(), "RGBA",
                                              "U8", "sRGB built-in",
                                              self.DPI.value())

        backgroundName = i18n("Background")
        if len(template.topLevelNodes()) > 0:
            backgroundNode = template.topLevelNodes()[0]
            backgroundNode.setName(backgroundName)
        else:
            backgroundNode = template.createNode(backgroundName, "paintlayer")
            template.rootNode().addChildNode(backgroundNode, None)

        pixelByteArray = QByteArray()
        if self.currentColor == Qt.white:
            pixelByteArray = backgroundNode.pixelData(0, 0, template.width(),
                                                      template.height())
            pixelByteArray.fill(int(255).to_bytes(1, byteorder='little'))
        else:
            red = int(self.currentColor.redF() * 255).to_bytes(
                1, byteorder='little')
            green = int(self.currentColor.greenF() * 255).to_bytes(
                1, byteorder='little')
            blue = int(self.currentColor.blueF() * 255).to_bytes(
                1, byteorder='little')
            alpha = int(self.currentColor.alphaF() * 255).to_bytes(
                1, byteorder='little')

            progress = QProgressDialog(i18n("Creating template"), str(), 0,
                                       template.width() * template.height())
            progress.setCancelButton(None)
            progress.show()
            qApp.processEvents()
            for byteNumber in range(template.width() * template.height()):
                pixelByteArray.append(blue)
                pixelByteArray.append(green)
                pixelByteArray.append(red)
                pixelByteArray.append(alpha)
                progress.setValue(byteNumber)
        backgroundNode.setPixelData(pixelByteArray, 0, 0, template.width(),
                                    template.height())
        backgroundNode.setOpacity(255)
        backgroundNode.setLocked(True)

        sketchNode = template.createNode(i18n("Sketch"), "paintlayer")
        template.rootNode().addChildNode(sketchNode, backgroundNode)

        verticalGuides = []
        verticalGuides.append(bL)
        verticalGuides.append(bL + mL)
        verticalGuides.append((bL + wBase) - mR)
        verticalGuides.append(bL + wBase)

        horizontalGuides = []
        horizontalGuides.append(bT)
        horizontalGuides.append(bT + mT)
        horizontalGuides.append((bT + hBase) - mB)
        horizontalGuides.append(bT + hBase)

        template.setHorizontalGuides(horizontalGuides)
        template.setVerticalGuides(verticalGuides)
        template.setGuidesVisible(True)
        template.setGuidesLocked(True)
        template.refreshProjection()

        self.urlSavedTemplate = os.path.join(self.templateDirectory,
                                             self.templateName.text() + ".kra")
        success = template.exportImage(self.urlSavedTemplate, InfoObject())
        print("CPMT: Template", self.templateName.text(), "made and saved.")
        template.waitForDone()
        template.close()

        return success