Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Processing XCF files in batch
#12
So it turns out there's one more problem I've discovered with some of the XCF files, where locking the alpha channel prevents some of the coloured pixels from being filled. I confirmed  this by opening the files manually in GIMP and trying to bucket fill them. Doing so with the alpha channel unlocked worked fine, but if I locked the alpha channel, they wouldn't get bucket-filled.

My guess is some of the red/green/blue pixels have non-0 non-255 values of alpha (I can't see alpha values in GIMP's colour picker tool though to confirm this). I have no idea how this was introduced to the images, but I have managed to replicate the issue using a layer mask. I've attached a sample project that shows the issue - bucket filling the Arm layer works fine, but if you lock the alpha channel, you can't fill it any longer.

Is there a way to adapt the script to handle this problem, so that partially transparent pixels in the layers are still filled and only the fully transparent pixels are ignored?

For reference, my current script looks like this in full (I've made some minor adjustments to address some other issues in the images, such as hidden layers, wrong layer names, and saved selections):

Code:
from gimpfu import *
import gimpcolor
import os
import os.path as osp
import sys


sys.stderr = open(osp.splitext(__file__)[0] + '.log', 'w')
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)
    pdb.gimp_selection_none(xcf)

    for layer in xcf.layers:
        layer.visible = True
        layer.opacity = 100.
        layer.lock_alpha = True
        if 'snek' in layer.name.lower():
            pdb.gimp_context_set_foreground(gimpcolor.RGB(255, 0, 0))
            pdb.gimp_edit_fill(layer, FILL_FOREGROUND)
        elif 'phone' in layer.name.lower():
            pdb.gimp_context_set_foreground(gimpcolor.RGB(0, 255, 0))
            pdb.gimp_edit_fill(layer, FILL_FOREGROUND)
        elif 'arm' in layer.name.lower():
            pdb.gimp_context_set_foreground(gimpcolor.RGB(0, 0, 255))
            pdb.gimp_edit_fill(layer, FILL_FOREGROUND)
        layer.lock_alpha = False

    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)


Attached Files
.xcf   Sample.xcf (Size: 721.58 KB / Downloads: 87)
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 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 Mate de Vita - 12-15-2022, 05:25 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: