Skip to content

Instantly share code, notes, and snippets.

@Larpon
Last active September 27, 2018 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Larpon/e81ed68895bdba35098d9d84688ac01e to your computer and use it in GitHub Desktop.
Save Larpon/e81ed68895bdba35098d9d84688ac01e to your computer and use it in GitHub Desktop.
Add PIL (pillow) package to a Krita 4 appimage

Dependant tools

pyenv

Quick install of pyenv (https://github.com/pyenv/pyenv-installer)

curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

appimagetool

wget https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage
mv appimagetool-x86_64.AppImage appimagetool
chmod +x appimagetool
sudo mv appimagetool /usr/local/bin/appimagetool

Procedure

Copy the krita-<version>-x86_64.appimage you want to patch to /tmp

cp /path/to/krita-4.1.3-x86_64.appimage /tmp

Channge working directory to /tmp

cd /tmp

Extract the app image using it's self-extract feature --appimage-extract extracts the squashfs-root which is in AppDir-format

./krita-4.1.3-x86_64.appimage --appimage-extract

Prepare a fresh python env

mkdir /tmp/krita-py
cd /tmp/krita-py
pyenv install 3.5.2

# Set python version to use in the "krita-py" folder
pyenv local 3.5.2

Install pillow in the custom python env (this could be any package)

pip install pillow

This part could be made much smarter. Maybe even add the /tmp/squashfs-root/usr/ as a pyenv directly or something.

Copy the installed package to the un-packed Krita appimage AppDir (here we copy the pillow/PIL package)

cp $PYENV_ROOT/versions/3.5.2/lib/python3.5/site-packages/PIL /tmp/squashfs-root/usr/lib/python3.5/site-packages/

Re-package the appimage

appimagetool /tmp/squashfs-root /tmp/krita-modded.appimage
chmod +x /tmp/krita-modded.appimage
/tmp/krita-modded.appimage

Download test file

wget -O /tmp/infile.png https://www.zamzar.com/images/filetypes/png.png

Now the following code should execute in the "scripter" Paste and run the following:

from PIL import PngImagePlugin
from PIL import Image, ImageDraw, ImageFile

def get_colors(infile, outfile, numcolors=10, swatchsize=20, resize=150):
    image = Image.open(infile)
    image = image.resize((resize, resize))
    result = image.convert('P', palette=Image.ADAPTIVE, colors=numcolors)
    result.putalpha(0)
    colors = result.getcolors(resize*resize)

    # Save colors to file

    pal = Image.new('RGB', (swatchsize*numcolors, swatchsize))
    draw = ImageDraw.Draw(pal)

    posx = 0
    for count, col in colors:
        draw.rectangle([posx, 0, posx+swatchsize, swatchsize], fill=col)
        posx = posx + swatchsize

    del draw
    pal.save(outfile, "PNG")


get_colors('/tmp/infile.png', '/tmp/outfile.png')

Now /tmp/outfile.png should have a bunch of blue-ish colors in it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment