Add mimetype setting to libkis to allow export via python.
ClosedPublic

Authored by woltherav on Apr 13 2017, 1:24 PM.

Details

Summary

Right now, the python module does not allow exporting to anything but kra because the output mimetype isn't set.

This patch adds a few lines to the export function in libkis, so that it will get the mimetype from the filepath and use that for the output mimetype.

Test Plan

This works for me. Just run the following script in scripter while having a non-png document open, and check if the output file is a proper png instead of secretly a kra file:

# Summary:
# A small script that saves the current image as a PNG with the same name.
# By Wolthera

# First, we import the relevant libraries.
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from krita import *

# We need 3 things to export.
# 1. we need to get the current document:
doc = Application.activeDocument()

# 2. we need to make a configuration object that stores the export options.
# The relevant export options were found in the C++ code for the PNG export.
export_config = InfoObject()
export_config.setProperty("alpha", True)
export_config.setProperty("interlaced", False)
export_config.setProperty("compression", 3)
export_config.setProperty("indexed", False)
export_config.setProperty("saveSRGBProfile", False)
export_config.setProperty("forceSRGB", True)

# 3. We need a path where to save the file.
# We make this path by first sticking the document filepath into a QFileInfo object.
# QFileInfo will deal with all the fiddly parsing things like getting the directory and the basename.
# We then paste those together to make a full path that contains a directory, a base file name and the extension.
file_path = QFileInfo(doc.fileName())
file_name = file_path.dir().absolutePath()+"/"+file_path.baseName()+".png"

# Finally, we tell the active document(1) to export using the filepath(3) and the export configuration(2) that we made.
# The export function returns a boolean, so we use that to make a nice little debug appear when the export was successful!
if (doc.exportImage(file_name, export_config)):
    print("Saved to " +file_name+", the export was succesful!")
else:
    print("Export failed :(")

Diff Detail

Repository
R37 Krita
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

That script seems to fix exporting out to PNG.

Currently if you run that script, the export works, but loading the file back into Krita has issues.

After applying the patch and running the script again, the PNG exports and it loads back into krita successfully

rempt accepted this revision.May 18 2017, 8:37 AM

Cleaning up my backlog... This can be pushed, of course.

This revision is now accepted and ready to land.May 18 2017, 8:37 AM
This revision was automatically updated to reflect the committed changes.