Skip to content

Instantly share code, notes, and snippets.

@MCOfficer
Last active January 21, 2023 13:11
Show Gist options
  • Save MCOfficer/bdf6c0c0935d22da38e72cc99fea6375 to your computer and use it in GitHub Desktop.
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
#!/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()
@MCOfficer
Copy link
Author

Thanks for letting your script public ! This helped me greatly for writing my own batch script :)

You're welcome, but be aware that this was written for an older gimp version, and it's very much a novice's work. For example, the section around L34 could be made significantly safer by using os.path.join instead of string concatenation.

@AronGomu
Copy link

Thanks for letting your script public ! This helped me greatly for writing my own batch script :)

You're welcome, but be aware that this was written for an older gimp version, and it's very much a novice's work. For example, the section around L34 could be made significantly safer by using os.path.join instead of string concatenation.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment