Last active
January 21, 2023 13:11
-
-
Save MCOfficer/bdf6c0c0935d22da38e72cc99fea6375 to your computer and use it in GitHub Desktop.
[Python-fu/Gimp] Vadim's Batch - written for https://stackoverflow.com/a/42933365/7653274
This file contains 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 | |
from gimpfu import * | |
import glob | |
import os | |
pdb = gimp.pdb | |
def vadimsbatch(loadfolder, fileextension, frame): | |
# correct loadfolder: add (back-)slash if needed | |
if not loadfolder.endswith(("/", "\\")): | |
if os.name == "nt": # we need backslashes on windows, but slashes on linux/mac | |
loadfolder = loadfolder + "\\" | |
else: | |
loadfolder = loadfolder + "/" | |
# prepare the file pattern | |
filepattern = loadfolder + fileextension | |
filelist = glob.glob(filepattern) | |
filelist.sort() | |
# gimp.message(" ".join(filelist) # for debugging | |
# loop once for every file | |
for filepath in filelist: | |
# load image | |
if filepath.endswith((".jpeg,", ".jpg")): | |
image = pdb.file_jpeg_load(filepath, filepath) | |
elif filepath.endswith(".png"): | |
image = pdb.file_png_load(filepath, filepath) | |
layer = image.active_layer | |
# prepare filename | |
if os.name == "nt": # we need backslashes on windows, but slashes on linux/mac | |
outputfolder = "%soutput\\" % loadfolder # add the name of a new folder | |
else: | |
outputfolder = "%soutput//" % loadfolder # add the name of a new folder | |
gimp.message(outputfolder) | |
if not os.path.exists(outputfolder): | |
os.makedirs(outputfolder) # create the new folder if it doesn't exist yet | |
filename = os.path.basename(filepath) # remove the path and only keep the actual filename with extension | |
outputpath = outputfolder + filename | |
# now the actual work | |
image.active_layer.scale(190, 120, 0, -10) # resize and move up | |
image.resize(190, 120, 0, 10) | |
#pdb.gimp_image_select_rectangle(image, 0, 0, -10, 190, 0) | |
#pdb.gimp_edit_cut(layer) | |
framelayer = pdb.gimp_file_load_layer(image, frame) # load the frame as new layer | |
image.add_layer(framelayer) | |
image.merge_visible_layers(0) | |
pdb.file_png_save_defaults(image, image.active_layer, outputpath, outputpath) | |
outputpath = "%s.xcf" % outputpath | |
pdb.gimp_xcf_save(0, image, image.active_layer, outputpath, outputpath) | |
register( | |
"vadimsbatch", | |
N_("*Creative Description*"), | |
"*Creative Description*", | |
"MCOfficer", | |
"@Copyright 2017", | |
"2017", | |
N_("_Vadim's Batch"), | |
"", | |
[ | |
(PF_STRING, "loadfolder", "The location of your textures.", ""), | |
(PF_STRING, "fileextension", "Files that should be loaded. Use * to load all files in this folder.", "*.jpg"), | |
(PF_FILE, "frame", "Select your frame", None) | |
], | |
[], | |
vadimsbatch, | |
menu="<Toolbox>/Scripts/", | |
domain=("gimp20-python", gimp.locale_directory) | |
) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, I did not have any problem with my own implementation and it's for personal use only. Your code gave me the template I needed. I have a hard time working off documentation only