Last active
January 3, 2021 22:19
-
-
Save fredrikaverpil/4942b48675b61c0759005f751098b4f5 to your computer and use it in GitHub Desktop.
Python in Krita
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import krita | |
def add_document_to_window(): | |
"""Add new document to Krita | |
Arguments: | |
width (int): width in pixels | |
height (int): height in pixels | |
name (str): name of the image (not the filename of the document) | |
colorModel (str): color model of document, e.g. "RGBA", "XYZA", "LABA", "CMYKA", "GRAYA", "YCbCrA" | |
colorDepth (str): color depth, e.g. "U8", "U16", "F16" or "F32" | |
profile (str): The name of an icc profile that is known to Krita | |
resolution (float): the resolution in points per inch | |
Returns: | |
the created document | |
""" | |
k = krita.Krita.instance() | |
doc = k.createDocument(100, 100, "Test", "RGBA", "U8", "", 120.0) | |
Application.activeWindow().addView(doc) | |
return doc | |
doc = add_document_to_window() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import os | |
import krita | |
from PyQt5 import QtWidgets | |
IMAGES_FILEPATH = QtWidgets.QFileDialog.getExistingDirectory() | |
FILEPATHS = sorted(glob.glob(IMAGES_FILEPATH + '/*')) | |
print(FILEPATHS) | |
def open_document(filepath, show=True): | |
"""Open the given filepath as new document | |
Arguments: | |
filepath (str): the filepath of the image to open | |
show (bool): should the image get shown in the Krita UI? | |
""" | |
k = krita.Krita.instance() | |
print('Debug: opening %s' % filepath) | |
doc = k.openDocument(filepath) | |
if show: | |
Application.activeWindow().addView(doc) | |
return doc | |
def get_layers(doc): | |
"""Return layers for given document""" | |
nodes = [] | |
root = doc.rootNode() | |
for node in root.childNodes(): | |
print('Debug: found node of type %s: %s' % (node.type(), node.name())) | |
if node.type() == "paintlayer": | |
nodes.append(node) | |
return nodes | |
def make_layered_psd_from_images(): | |
"""Takes a folderpath, scans it for images and produces a layered image""" | |
doc = open_document(FILEPATHS[0], show=False) | |
doc_root = doc.rootNode() | |
docs = [] | |
docs.append(doc) | |
all_layers = get_layers(doc) | |
for i in range(1, len(FILEPATHS)): | |
docx = open_document(FILEPATHS[i], show=False) | |
docs.append(docx) | |
docx_layers = get_layers(docx) | |
for layer in docx_layers: | |
all_layers.append(layer.clone()) | |
# doc.rootNode().addChildNode(layer, parent_node) | |
doc_root.setChildNodes(all_layers) | |
print('Debug: all nodes: %s' % doc.rootNode().childNodes()) | |
# doc.refreshProjection() | |
save_filepath = filepath = QtWidgets.QFileDialog.getSaveFileName()[0] | |
r = doc.saveAs(save_filepath) | |
print('Debug: saved: %s' % save_filepath) | |
for doc in docs: | |
print('Debug: closing %s' % doc) | |
doc.close() | |
print('Debug: Script done') | |
make_layered_psd_from_images() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import krita | |
from PyQt5 import QtWidgets | |
def open_document(filepath): | |
"""Open the given filepath as new document""" | |
k = krita.Krita.instance() | |
doc = k.openDocument(filepath) | |
Application.activeWindow().addView(doc) | |
return doc | |
filepath = QtWidgets.QFileDialog.getOpenFileName()[0] | |
doc = open_document(filepath) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import krita | |
from krita import InfoObject | |
from PyQt5 import QtWidgets | |
k = krita.Krita.instance() | |
doc = k.activeDocument() | |
if doc is not None: | |
filepath = QtWidgets.QFileDialog.getSaveFileName()[0] | |
doc.exportImage(filepath, InfoObject()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, found this: https://www.reddit.com/r/krita/comments/7z73fj/saving_layer_groups/