|
|
(8 intermediate revisions by 5 users not shown) |
Line 1: |
Line 1: |
| Krita Scripting | | = Krita Scripting = |
|
| |
|
| ==Intro==
| | As of version 4.0, Krita supports scripting via the Python programming language. Please see the [https://docs.krita.org/en/user_manual/python_scripting.html user manual] for instructions on how to use the feature. |
| | |
| ===Krita and Scripting===
| |
| | |
| [http://www.koffice.org/krita/ Krita] is a painting and image editing application for KOffice.
| |
| | |
| Krita comes with a bunch of [http://websvn.kde.org/trunk/koffice/krita/plugins/ plugins] where the [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/ scripting plugin] uses the [http://kross.dipe.org Kross] scripting framework to offer powerful scripting with [http://www.python.org Python], [http://www.ruby-lang.org Ruby] and [http://xmelegance.org/kjsembed KDE JavaScript].
| |
| | |
| The plugin consist of following parts;
| |
| | |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scriptingpart.h?view=markup ScriptingPart] class which implements a KParts plugin that is loaded by Krita on startup and which is responsible for displaying the [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scriptingdocker.h?view=markup ScriptingDocker] and the setup of the scripting environment.
| |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/kritacore/ kritacore module] offers an interface to scripting backends to deal with the Krita internals. The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/kritacore/krs_module.h?view=markup Scripting::Module] class is the entry point for your script to communicate with Krita.
| |
| | |
| ===Scripting Handbook===
| |
| | |
| The [http://kross.dipe.org/dox/krita.html Krita Scripting Handbook] contains a full reference of all objects and methods accessible from within the scripting backends.
| |
| | |
| The Handbook is generated from the sourcecode using doxygen and the [http://websvn.kde.org/trunk/KDE/kdelibs/kross/docs/doxy2doc.py?view=markup doxy2doc.py] Python script as postprocessor to create visible output from the by doxygen produced XML files.
| |
| | |
| ===Links===
| |
| | |
| * [http://www.koffice.org/krita/ Krita Homepage]
| |
| * [http://wiki.koffice.org/index.php?title=Krita Krita Wiki]
| |
| * [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/ Krita Scripting Plugin code]
| |
| * [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripting.dox?view=markup Krita Scripting Plugin ReadMe]
| |
| * [http://kross.dipe.org Kross Homepage]
| |
| * [http://wiki.koffice.org/index.php?title=Kross Kross Wiki Page]
| |
| * [http://wiki.koffice.org/index.php?title=Kross/Tutorial Kross Tutorial]
| |
| | |
| ==Scripting Extensions==
| |
| | |
| Extensions are used to extend Krita with additional functionality written in Python, Ruby or KDE JavaScript
| |
| scripts.
| |
| | |
| The following screenshot shows Krita running with it's on the right half displayed scripting-docker that is used to display scripts and fastly execute scripts. The image itself was created using the [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/randompaint.rb?view=markup Random painting] Ruby script.
| |
| | |
| [[Image:krita2scripting_screeny1.jpg]]
| |
| | |
| Those extensions are also accessible from within the "Scripts"-menu and are distributed with Krita, so some
| |
| default extensions are installed together with Krita as part of it, or could be later added and configured
| |
| on demand using the "Script Manager".
| |
| | |
| [[Image:krita2scripting_screeny2.jpg]]
| |
| | |
| Following sections try to provide an overview of the per default with Krita distributed scripting extensions.
| |
| | |
| ===Import and Export===
| |
| | |
| The both scripts [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/pilimport.py?&view=markup pilimport.py] and [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/pilexport.py?&view=markup pilexport.py] are using the [http://www.pythonware.com/library/ Python Imaging Library] to import and export to the by PIL support image-formats.
| |
| | |
| The following screenshot shows the "Python Imaging Library Import" Python script [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/pilimport.py?&view=markup pilimport.py] once executed.
| |
| | |
| For GUI-related things you are also able to use [http://www.riverbankcomputing.co.uk/pyqt/ PyQt4] or [http://docs.python.org/lib/module-Tkinter.html Tkinter] in your python scripts, [http://rubyforge.org/projects/korundum/ Korundum/QtRuby] in your Ruby scripts, [http://xmelegance.org/kjsembed/ KjsEmbed4] (KjsEmbded4 is included in kdelibs4 now) in your JavaScript scripts or the more high-level [http://websvn.kde.org/trunk/KDE/kdelibs/kross/modules/form.h?view=markup Kross forms] in all interpreter-backends.
| |
| | |
| [[Image:krita2scripting_screeny33.jpg]]
| |
| | |
| Most parts of the [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/pilimport.py?&view=markup pilimport.py] Python script are related to GUI and preparation of the import-proccess. The core part of that script, so the part that does actualy do the import, looks like;
| |
| | |
| <pre>
| |
| # import needed modules and initialize PIL
| |
| import Krita, Image, ImageFile
| |
| Image.init()
| |
| # read the imagefile and convert to RGB
| |
| pilimage = Image.open("/home/user/myimage.jpg")
| |
| pilimage = pilimage.convert("RGB")
| |
| # fetch the Krita layer we like to import to
| |
| krtlayer = Krita.image().activePaintLayer()
| |
| # scale the loaded image to fit
| |
| pilimage = pilimage.resize(
| |
| (krtlayer.width(), krtlayer.height()))
| |
| # let's use the progressbar
| |
| Krita.progress().setProgressTotalSteps(
| |
| krtlayer.width() * krtlayer.height())
| |
| # finally do the import
| |
| krtlayer.beginPainting("PIL import")
| |
| it = krtlayer.createRectIterator(
| |
| 0, 0, krtlayer.width(), krtlayer.height())
| |
| while (not it.isDone()):
| |
| data = pilimage.getpixel((it.x(), it.y()))
| |
| it.setPixel(list(data))
| |
| Krita.progress().incProgress()
| |
| it.next()
| |
| krtlayer.endPainting()
| |
| </pre>
| |
| | |
| The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/pilexport.py?&view=markup pilexport.py] Python script which does export a Krita image to a by the Python Imaging Library supported image-format looks then like;
| |
| | |
| <pre>
| |
| # import needed modules and initialize PIL
| |
| import Krita, Image, ImageFile
| |
| Image.init()
| |
| # fetch the Krita layer we like to export
| |
| krtlayer = Krita.image().activePaintLayer()
| |
| # create the PIL image
| |
| pilimage = Image.new("RGB",
| |
| (krtlayer.width(), krtlayer.height()))
| |
| # finally do the export
| |
| it = krtlayer.createRectIterator(
| |
| 0, 0, krtlayer.width(), krtlayer.height())
| |
| finesh = it.isDone()
| |
| while (not finesh):
| |
| pilimage.putpixel(
| |
| (it.x(),it.y()), tuple(it.pixel()))
| |
| finesh = it.next()
| |
| # and save the result
| |
| pilimage.save("/home/user/myimage.jpg")
| |
| </pre>
| |
| | |
| ===Painting===
| |
| | |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/randompaint.rb?view=markup randompaint.rb] Ruby script demonstrates how to use the Painter scripting API and paints random shape on the screen. The image as shown at the first screenshot above was made using the script.
| |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/torture-painting.rb?view=markup torture-painting.rb] Ruby script tortures Krita with painting. Take care, this script may run a very long time.
| |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/changecs.rb?view=markup changecs.rb] Ruby script demonstrates how to change the colorspace.
| |
| | |
| The following three sample scripts are doing the exactly the same thing, that is invert the pixel of an image, and are written code-wise the same way while different scripting backends are used. It is a nice to compare the languages and to show, that the same rich API is accessible to all of them :)
| |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/invert.rb?view=markup invert.rb] Ruby script inverts the pixel of an image.
| |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/invert.py?view=markup invert.py] Python script inverts the pixel of an image.
| |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/invert.js?view=markup invert.js] JavaScript script inverts the pixel of an image.
| |
| | |
| ===Filters===
| |
| | |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/filterstest.rb?view=markup filterstest.rb] Ruby script tests the Krita filters.
| |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/torture-filters.rb?view=markup torture-filters.rb] Ruby script tortures Krita with filters.
| |
| | |
| ===Histogram===
| |
| | |
| * The [http://websvn.kde.org/trunk/koffice/krita/plugins/viewplugins/scripting/scripts/reshapehisto.py?view=markup reshapehisto.py] Python script is an experimental script to try to reshape histogram, while the results are far to be convincing yet it is still an example on how to use histogram in a script.
| |