This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Questions regarding Python API

Tags: None
(comma "," separated)
User avatar
rbreu
Registered Member
Posts
52
Karma
0

Questions regarding Python API

Wed Dec 19, 2018 9:17 pm
I'm writing a Python plugin that automates the process of scaling down an image for export, exporting, and undoing the scaling. It works great with the following code:

Code: Select all
self.app.action('imagesize').trigger()
self.app.action('file_export_file').trigger()
self.app.action('edit_undo').trigger()


There's one problem, though: If the user cancels out of the scale dialog, I shouldn't perform an undo at the end. I'm not quite sure how I can find out whether or not I need to do the undo. The trigger-Method always seems to return None no matter what. One idea I had was to check the undo history before and after, but I can't find out how to access it. Any ideas?
ahabgreybeard
Registered Member
Posts
1269
Karma
11
OS

Re: Questions regarding Python API

Wed Dec 19, 2018 10:40 pm
If you can't get access to the undo history, you could perform what is effectively a null-action (as far as the image is concerned) before you do the imagesize. A good one would be Select All (if you can do it) which will have no effect on the image and which will be replaced by any further selection in future. That way, if the scale dialogue is cancelled, the edit_undo will just undo the Select All. It's a 'cheat' but it should work.
User avatar
rbreu
Registered Member
Posts
52
Karma
0

Re: Questions regarding Python API

Thu Dec 20, 2018 7:45 am
Hmm. Selecting is not really a null operation, though. The selection gets saved in the image, and accordingly, selecting something sets the state of an unmodified image to modified. Ideally, I'd like to cleanly go back to the image's modified state from before. (There's currently a bug regarding modified state and undoing, but I'd still like the script to do the right thing.)

But it's probably better than what I have now, at least for my personal use.
hulmanen
Registered Member
Posts
33
Karma
1

Re: Questions regarding Python API

Fri Feb 08, 2019 9:34 am
I'm looking to create an "export selection" extension as well, fairly similar to what you need I think. I wonder if it's a good idea to scale the document you're working on and then undo that? I'd rather not do a destructive edit on a work file, in case something goes wrong. So perhaps it would be better to copy the data you want to export into a new temp document, do your operations on that, and close it once exported. This would also remove the need to worry about undo levels.
User avatar
rbreu
Registered Member
Posts
52
Karma
0

Re: Questions regarding Python API

Fri Feb 08, 2019 6:06 pm
That was actually my first idea. The thing is, though, I wanted to re-use the existing scale and export dialogs instead of coding my own (especially the scale dialog has a lot of stuff going on), and they are only accessible via the actions who always work on the current/active document. So you'd have to make a copy of the document, make that the current/active document, and remove that again, which isn't quite invisible to the user either. Though I hadn't actually tried that and don't know how it feels from a user perspective. I might do that, now that I've got a little more comfortable with the API! As it is, I'm only using the script for myself and I know how it works, but it would be neat though to enhance it enough to be able to advertise it. ;)

Though if you get your script to work, I guess I could do my use case (export the whole image) with that as well by selecting the whole image. :D
User avatar
TheraHedwig
KDE Developer
Posts
1794
Karma
10
OS

Re: Questions regarding Python API

Fri Feb 08, 2019 6:50 pm
Here you go, the following code saves out the part of the selection in the image.

It does this by copying the pixeldata of the document projection within the selection to a paint layer in a new document, then it asks for a filename with qfiledialog and saves the file. Finally it closes the document.

You can do a version where it scales the dstDoc too, but you'll need to copy the whole document projection, add node, then scale, then destDoc.waitForDone() and then refresh, save, close.

Code: Select all
from krita import *
from PyQt5.QtWidgets import QFileDialog

# Get the active document and it's selection.
doc =  Application.activeDocument()
sel = doc.selection()


# check if the selection is valid.
if sel is not None:
   
    # Call up QFileDialog to get a save location. This is a tuple, but we only need the first entry.
    filename = QFileDialog.getSaveFileName(caption="Save File", filter="Images (*.png *.xpm *.jpg)")[0]

    # then, make a new document using selection widht and height but doc colorspace info and resolution.
    destDoc = Application.createDocument(sel.width(), sel.height(), "selection", doc.colorModel(), doc.colorDepth(), doc.colorProfile(), doc.resolution())
    # Create a new paintlayer.   
    newNode = destDoc.createNode("paintlayer", "paintlayer")

    # Copy the pixel data from the paintlayer using the selection info.
    ba = doc.pixelData(sel.x(), sel.y(), sel.width(), sel.height())
    newNode.setPixelData(ba, 0, 0, sel.width(), sel.height())

    # Add paintlayer to new document and refresh the projection.
    destDoc.rootNode().addChildNode(newNode, None)   
    destDoc.refreshProjection()
   
    # Use the filename and save, and finally close the document.
    destDoc.setFileName(filename)
    print("saving to", filename)
    destDoc.save()
    destDoc.close()


It's generally easier to do everything in a copy of the active document when exporting so you don't have to mess with the undo.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]