Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Processing XCF files in batch
#9
(12-09-2022, 09:02 PM)rich2005 Wrote: Well, I repeat, my scripting ability is very basic.

The attached zip contains 3 files. color-exchange (linux),  color-exchange.exe (win) , and a very simple two line plugin swap-rgb.py

Put the appropriate color-exchange & swap-rgb.py in your user plugins folder. swap-rgb.py registers in the Tools menu.

Fortunately your example uses rgb(255,0,0) red and rgb(0,255,0) green  the layer position is in  "image.layers[x] and works for your example.xcf. Edit as required.

Works here Gimp 2.10.32 / kubuntu 20.04 (providing you have a working gimp-python ) and Gimp 2.10.32 / Win 10 (VM)

How to put this into a batch file, I do not know. Might / might not help you.

Thank you, but as far as I can tell this still only works for a single project/image, so the batch processing is still necessary. Additionally, unfortunately I've found that some of the files have pixels that don't have the exact colour values (255, 0, 0) or (0, 255, 0), but instead have something close, like (252, 0, 0). So I'd like to instead fill all the coloured pixels in the two layers with the correct colour (so for instance also replace (252, 0, 0) with (0, 255, 0)).

(12-10-2022, 07:39 AM)Ofnuts Wrote: Several things:
  • Your "external" script in PythonV3. Keep in mind that the Python using by Gimp is PythonV2 (so that's the old print syntax and there are no f-strings).  If you abide to this then print statements do send output to the Gimp process StdOut and on Linux/OSX if you start Gimp from a terminal this is readily readable. Things are a bit more complicated on windows but there are ways.
  • You also have to make sure that you have Python support in Gimp (presence of Filters ➤ Python-fu ➤ Console). Not too clear on which OS you are but on Windows installing both Gimp and an external Python interpreter can mess up things for Gimp.
  • Using the shell=True form is somewhat risky, given the nesting of quotes you have to deal with.
  • You are importing moduleName.py, not moduleName
  • NORMAL_MODE is not really valid for merge_visible_layers(). That should be EXPAND_AS_NECESSARY (same value, but better vibes)
  • If would be a lot better to pass a directory to scan for Gimp and to iterate the files inside the Gimp script. With the current form you are potentially paying the price of a Gimp startup for every file (unless there is already a Gimp instance running)

So I've gone through this and adapted my approach significantly, as well as adding the colour correction mentioned above to the blue layer too. I now call my GIMP script using:
Code:
scrpath = 'path/to/script'
scrname = 'fix_projects'
xcfdir = 'dir/of/xcf/files'
savedir = 'dir/to/save/new/xcf/files/to'
pngdir = 'dir/to/export/png/files/to'
pycode = f'import sys; sys.path.insert(0, "{scrpath}"); import {scrname}; {scrname}.run("{xcfdir}", "{savedir}", "{pngdir}")'
subprocess.run(['gimp', '-idf', '--batch-interpreter', 'python-fu-eval', '-b', pycode, '-b', 'pdb.gimp_quit(1)'], check=True)

And this is now my GIMP script:
Code:
from gimpfu import *
import gimpcolor
import os
import os.path as osp
import sys


sys.stderr = open(osp.splitext(__file__)[0] + '.log', 'a')
sys.stdout = sys.stderr


def run(project_dir, save_dir, export_dir):
    for root, _, fnames in os.walk(project_dir):
        for fname in fnames:
            bname, ext = osp.splitext(fname)
            if ext.lower() != '.xcf':
                continue

            project_f = osp.join(root, fname)
            rel_path = osp.dirname(osp.relpath(project_f, project_dir))
            save_f = osp.join(save_dir, rel_path, bname + '.xcf')
            export_f = osp.join(export_dir, rel_path, bname + '.png')
            fix_project(project_f, save_f, export_f)


def fix_project(project_f, save_f, export_f):
    print project_f
    xcf = pdb.gimp_xcf_load(0, project_f, project_f)
    for layer in xcf.layers:
        layer.opacity = 100.
        if layer.name == 'Currently Green':
            pdb.gimp_context_set_foreground(gimpcolor.RGB(255, 0, 0))
            layer.fill(FOREGROUND_FILL)
        elif layer.name == 'Currently Red':
            pdb.gimp_context_set_foreground(gimpcolor.RGB(0, 255, 0))
            layer.fill(FOREGROUND_FILL)
        elif layer.name == 'Currently Blue':
            pdb.gimp_context_set_foreground(gimpcolor.RGB(0, 0, 255))
            layer.fill(FOREGROUND_FILL)

    if not osp.isdir(osp.dirname(save_f)):
        os.makedirs(osp.dirname(save_f))
    if not osp.isdir(osp.dirname(export_f)):
        os.makedirs(osp.dirname(export_f))
    pdb.gimp_xcf_save(0, xcf, None, save_f, save_f)
    xcf.merge_visible_layers(EXPAND_AS_NECESSARY)
    pdb.file_png_save(xcf, xcf.layers[0], export_f, export_f, 0, 9, 1, 0, 0, 1, 1)
    pdb.gimp_image_delete(xcf)

This code now runs properly and produces the desired log file, as well as the .xcf and .png outputs. However, this current version fills the entire layers with the colours so the end image just ends up being all blue (since the blue layer is on top). How would I fill only the parts of the layer that are already coloured and not transparent?
Reply


Messages In This Thread
Processing XCF files in batch - by Mate de Vita - 12-01-2022, 03:38 PM
RE: Processing XCF files in batch - by Ofnuts - 12-02-2022, 05:48 PM
RE: Processing XCF files in batch - by Ofnuts - 12-10-2022, 07:39 AM
RE: Processing XCF files in batch - by Mate de Vita - 12-11-2022, 11:53 AM
RE: Processing XCF files in batch - by rich2005 - 12-09-2022, 04:53 PM
RE: Processing XCF files in batch - by rich2005 - 12-09-2022, 09:02 PM
RE: Processing XCF files in batch - by Ofnuts - 12-11-2022, 04:55 PM
RE: Processing XCF files in batch - by Ofnuts - 12-16-2022, 08:18 AM
RE: Processing XCF files in batch - by Ofnuts - 12-16-2022, 12:54 PM

Forum Jump: