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

Python plugins and external libraries

Tags: None
(comma "," separated)
bltpyro
Registered Member
Posts
1
Karma
0
Is there a good way to use external python libraries within plugins? Ones like opencv, pillow, scikit-image, etc? I'm not sure if this is the right sub-forum for it, but it seemed to fit the most. I can't seem to find any information one way or the other about it.
User avatar
halla
KDE Developer
Posts
5092
Karma
20
OS
I actually am not sure. I'm hoping that some people who are current with their python expertise will weigh in and help us figure out how to do this properly.
saedjubarin
Registered Member
Posts
58
Karma
0
This is my first pykrita test, I revised it without testing:

Code: Select all
# I was planning to use BytesIO for it's memory buffer but I lose the benefits halfway through
from io import BytesIO
from PIL import Image, ImageMath

doc = Krita.activeDocument()
H, W, nodes = doc.height(), doc.width(), doc.topLevelNodes()

# Since I paint most of my brush tips with just black, only alpha channel is touched. The RGB values are already 0,0,0. Sometimes I'm not sure if I went full 255 in the darkest pixel, so let's go through all the top level nodes and make sure the alpha is evenly stretched to hit the max.

for node in nodes:
    node.setVisible(True)
    print(node.name())
    pixeldata = BytesIO(node.pixelData(0,0,H,W)) # could use unpack from struct maybe?
    pixeldata = pixeldata.getbuffer()
    pd_max = max(pixeldata)

    # Pillow the image
    mode, size, data = "RGBA", (H, W), pixeldata
    layer = Image.frombuffer(mode, size, data)

    # this is the magic part, pillow stretches the alpha values so that they're up to 255
    R, G, B, A = layer.split()
    # ... or anything else you might want to achieve
    A = ImageMath.eval("convert(a*(255//b), 'L')", a=A, b=pd_max) # ImageMath only likes single channel.
    layer = Image.merge("RGBA", (R, G, B, A))
    # PIL.Image.tobytes() to get back to pixelData
    node.setPixelData(layer.tobytes(), 0,0,1024,1024)

    # since we made changes we want to see them too
    doc.refreshProjection()


I hope you like it.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Evergrowing, Google [Bot], rblackwell