An example
from krita import *
import re
fileName="/home/grum/test.kra"
print('================================================= EXAMPLE 1 =================================================')
doc = Krita.instance().createDocument(300, 300, "Test", "RGBA", "U8", "", 300.0)
data1="""gam_p1s1=0.5,0.1,0.9,0.5,0.5,0.9,0.1,0.5
gam_p1s3=0.5,0.1,0.84641,0.7,0.15359,0.7
gam_p1s4=0.5,0.1,0.9,0.5,0.5,0.9,0.1,0.5
gam_p2s1=0.5,0.1,0.675,0.275,0.5,0.45,0.325,0.275,0.5,0.55,0.675,0.725,0.5,0.9,0.325,0.725
gam_p3s3=0.5,0.5,0.5,0.15359,0.8,0.32679,0.8,0.67321,0.5,0.84641,0.2,0.67321,0.2,0.32679"""
doc.setAnnotation('my_plugin_reference_annotation_key1', "my annotation description", QByteArray(data1.encode()))
doc.saveAs(fileName)
doc.close()
doc=Krita.instance().openDocument(fileName)
for annotation in doc.annotationTypes():
if re.match('my_plugin_reference_annotation_key\d+', annotation):
ba=doc.annotation(annotation)
print("Description: ", doc.annotationDescription(annotation))
print(f"Content:\n---\n{bytes(ba).decode()}")
doc.close()
# or if you prefer to have one key per data for example:
print('================================================= EXAMPLE 2 =================================================')
items=data1.split('\n')
doc = Krita.instance().createDocument(300, 300, "Test", "RGBA", "U8", "", 300.0)
for item in items:
if r:=re.match("gam_([^=]+)=", item):
doc.setAnnotation(f'my_plugin_reference_annotation_key_{r.groups()[0]}', f"my annotation description ({r.groups()[0]})", QByteArray(item.encode()))
doc.saveAs(fileName)
doc.close()
doc=Krita.instance().openDocument(fileName)
for annotation in doc.annotationTypes():
if re.match('my_plugin_reference_annotation_key.+', annotation):
ba=doc.annotation(annotation)
print("Description: ", doc.annotationDescription(annotation))
print(f"Content: '{bytes(ba).decode()}'\n")
doc.close()
but be aware that currently, there’s a bug with annotation system
https://bugs.kde.org/show_bug.cgi?id=441704
It’s possible to work with it, but in some specific situation, data can be lost.
Grum999