A for loop to make png from all krita documents from the commande line

I am looking for a way to do a for loop - with a bash script - to make png’ s from all of my Krita documents, from the command line. This would be handy, instead of to open every single Krita document, and export it to a png by hand.

System: Krita on Linux PopOs, Meerkat System76, Xp Pen Artist Pro 24.

1 Like
#!/bin/bash

# get current working directory
export cwd="$PWD"

# create folder for output
mkdir -p output

# create a folder in tmp for unzipping contents of kra file
mkdir -p /tmp/kra-output

# start the loop for each file in current working directory (cwd)

for f in *.kra
do
echo "processing file - $f"

#unzip the merged png image inside kra file
unzip -j "$f" "mergedimage.png" -d "/tmp/kra-output/" > /dev/null 2>&1

chmod 664 /tmp/kra-output/mergedimage.png

# convert the image in to srgb and remove alpha etc - based on @deevad 's render farm script this needs imagemagick package
convert /tmp/kra-output/mergedimage.png -colorspace sRGB -background white -alpha remove "$cwd"/output/"${f%.*}".png

#remove the output image (need to find better way to do this)
rm /tmp/kra-output/mergedimage.png
done

#delete the tmp folder
rmdir /tmp/kra-output
echo "finished exporting"

I would advise you to read the code and do a trial run, I will not take the responsibility of any damage or file deletion. Although I doubt it will happen since I use the same script too.

You can also modify the script to use Krita’s own commandline option rather than imagemagick, you can find the information about it here.

1 Like

Thank you for your fast and quick and working answer! Yes this script looks good, and will do the job.

I changed the rm rf to rmdir to be on the safe side

Wise to do so, I will make that change.

What in Krita makes it such a complex for loop, is there not a direct way to go from kra to png on the command line? So that a simple for loop will do the job. For example Inkscape give you this this function, on the command line.

Yeah there is a command line option for krita too like inkscape see the last paragraph in my answer :slight_smile:

1 Like

for f in *.kra ; do krita "$f" --export --export-filename "$f.png" ;done

Thanks for your help, this simple line will do the job

1 Like

Honestly it is easier to make a natron file to do this.

Wel explain what is a natron file?

if you want the long answer, it is a file created by the Natron program.

Honestly, it’s easier to write one command line than using Natron.
Especially when user specifically asked for a bash script solution.

Using Natron for this is like using a bulldozer in your garden rather than a shovel to dig a hole to plant some flowers…

Grum999

1 Like

your kinda ignoring what this type of program is meant to do. it does the work of the shovel and the bulldozer it is that malleable.

not to mention as much as you have a script in this case, nodes are just a GUI representation of a script you make, that you can easily reuse for any source. you can make a natron file once just like this script and then reuse it again.

Honestly the only issue I see for Natron here is openning the *.kra that I did not read as the source as I glanced over this thread. I thought it was some JPG’s or something. But their might be a plugin for it.

Regardless this should be work for nodes not a script. Scripts are much more annoying to reuse than nodes.

P.S. My bad it does open KRA files. XD

Not sure to understand your arguments, that’s stay a nonsense to me according to @KritPopCha question :man_shrugging:

Grum999

I am not answering the question because it was already answered, and honestly I did not even know Krita had a command line feature as it seems locked for Windows. but I read the code and honestly I would never do it like that myself for several reasons.
i was just saying there is a better way to go about this issue in my mind, no one is forced to agree with me.

Isn’t .APNG different of .PNG?

Yep, it is an AnimatedPortableNetworkGraphic.

Michelist

So @KritPopCha is creating/outputing .PNG files that are .PNG and animated, with no need for the .APNG extension?

Even though this seems to be quite off-topic (*), I’ll try to answer your question, since @KritPopCha’s problem is already solved it shouldn’t bother.

I have no idea how you come to this conclusion. @KritPopCha has *.KRA files that he wants to convert to another format (PNG). @KritPopCha was looking for a way to automate this task for himself, because @KritPopCha didn’t want to open every single image in Krita only to save it as *.PNG file directly afterwards.
If I imagine I would have 100, or more KRA files that I would want to save as a PNG file, then I would also look for an automated way to do this.
I assume you are inferring animation because of the word “loop”, but here the word “loop” refers to its programming meaning. The (multiple) repeat of a program part, or also a script, one calls in the surrounding field of programming / of software “loop”!
But, why are you breaking @KritPopCha’s head, do you have a better suggestion, or did you really believe in another PNG animation format? That’s what I don’t understand? :thinking:

Michelist

(*) Actually, you would have been better off creating your question on this as a new topic, with a link to this topic, or the post you are referring to.

1 Like

Ooh, got it now. Yeah, thought the “loop” meant animation. No no, I only got interested believing it was a post about animated .PNG files. That’s all. I’ll create a new topic for that matter in specific. Since I never knew it was even possible to create aninations in .PNG format. Thx for the feedback anyways.

Well, I just checked, and I can tell you that Krita together with FFmpeg is also able to save APNG animations. But everything else about animations will have to be explained to you by other forum members, I don’t create animations myself and don’t know anything about it. But to show you that this is possible, I rendered an animation from @AhabGreybeard as APNG file.

So that the forum accepts the file, I have removed the A from the file extension APNG and an APNG obviously works even if you steal its A! :slight_smile: (which I did not know before)

Michelist

2 Likes