Script for saving kra document and exporting to png simultaneously with incremental file naming

Needed a more seamless way to save my documents and export them as png files so i got inspired by user alfredbaudisch’s post
to bypass export dialog and save the two files with one script.
This script saves the files under the name ‘file+incremental number’ but only if the active document is unsaved. If you open the document under a different name/change the name of the document, that name will not be changed and the png file will be created under the same name.

Script

  
from krita import *
import os.path
from pathlib import Path


currentDocument = Krita.instance().activeDocument()
currentDocument.setBatchmode(True) # do not display export dialog box

pngOptions=InfoObject()
pngOptions.setProperty('compression', 2) # 0 (no compression) to 9 (max compression)
pngOptions.setProperty('indexed', False)
pngOptions.setProperty('interlaced', False)
pngOptions.setProperty('saveSRGBProfile', False)
pngOptions.setProperty('forceSRGB', True)
pngOptions.setProperty('alpha', True)


fileName = currentDocument.fileName()

if fileName == '':
    serialName = # insert path/file
    serialNumber = 0
    fileName=serialName+str(serialNumber)+'.kra'
    while Path(fileName).is_file():
        serialNumber+=1
        fileName=serialName+str(serialNumber)+'.kra'


        
currentDocument.saveAs(fileName)
nameExt=fileName.split('.')
newDocument = currentDocument.exportImage(nameExt[0]+'.png',pngOptions)


Usage

The script need to be saved as a .py document and then ‘imported’ through Tools>Scripts>Ten scripts

To make it easier to use I changed the default first script shortcut (Shift+Cmd+1) to the default saving shortcut (Cmd+s), this can be done in Krita>Preferences>Keyboard Shortcuts as shown below.

I found that this worked without issues for my needs but feel free to let me know if any particular issue arises.

3 Likes

:slight_smile: Hello @Kernel, and welcome to the forum!

Thank you for sharing your script with the community!

Michelist

1 Like

Welcome to the forum, @Kernel. And thank you for sharing this work with us.

1 Like