Last active
December 29, 2016 10:46
-
-
Save plexigras/d07a37ccb6657202457dc3c6f0b7a600 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: iso-8859-15 -*- | |
# GIMP script for several stroke-editing functions | |
# (c) plexigras 2016 | |
# | |
# History: | |
# | |
# v0.0: 2016-12-18 Original version by Ofnuts https://sourceforge.net/projects/gimp-tools/files/scripts/ | |
# v0.1: 2016-12-25 Customizations by plexigras | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published | |
# by the Free Software Foundation; either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This very file is the complete source code to the program. | |
# | |
# If you make and redistribute changes to this code, please mark it | |
# in reasonable ways as different from the original version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# The GPL v3 licence is available at: https://www.gnu.org/licenses/gpl-3.0.en.html | |
import os,os.path,collections,imghdr | |
from gimpfu import * | |
def createImage(imageName,d,dirs,output,root): | |
allImages=[[os.path.join(root,s,d,imageName),s] for s in sorted(dirs)] | |
baseImage=allImages[0] | |
layerImages=allImages[1:] | |
baseName,_=os.path.splitext(imageName) | |
outputImage=os.path.join(output,d,baseName+'.xcf') | |
image=pdb.gimp_file_load(baseImage[0],baseImage[0]) | |
image.active_drawable.name=baseImage[1] | |
layer=image.active_drawable | |
for layerImage in layerImages: | |
layer=pdb.gimp_file_load_layer(image,layerImage[0]) | |
image.add_layer(layer,0) | |
layer.name=layerImage[1] | |
pdb.gimp_xcf_save(0,image,layer,outputImage,outputImage) | |
def coalesceImages(root,output): | |
subdirs=[o for o in os.listdir(root) if os.path.isdir(os.path.join(root,o))] | |
allfiles=collections.defaultdict(lambda: []) | |
for o in subdirs: | |
os.chdir(os.path.join(root,o)) | |
for d,dirs,files in os.walk("."): | |
if not allfiles[d]: | |
allfiles[d]=collections.defaultdict(lambda: []) | |
for f in files: | |
if(imghdr.what(os.path.join(d,f))): | |
allfiles[d][f].append(o) | |
for d in sorted(allfiles.keys()): | |
path=os.path.join(output,d) | |
if not os.path.isdir(path): | |
os.mkdir(path) | |
for f in allfiles[d]: | |
createImage(f,d,allfiles[d][f],output,root) | |
register( | |
'coalesce-images', | |
'Coalesce images with same name in a directory tree into a single XCF', | |
'Coalesce images with same name in a directory tree into a single XCF', | |
'plexigras','plexigras','2016', | |
'Coalesce images', | |
'', | |
[ | |
(PF_DIRNAME, 'root', 'Root of image directories', '.'), | |
(PF_DIRNAME, 'output', 'Output directory', '.') | |
],[], | |
coalesceImages, | |
menu='<Image>/File/' | |
) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment