Skip to content

Instantly share code, notes, and snippets.

@HerbFargus
Created May 6, 2021 17:13
Show Gist options
  • Save HerbFargus/46c3b34091e31e0d81d9d9a3f41f1c41 to your computer and use it in GitHub Desktop.
Save HerbFargus/46c3b34091e31e0d81d9d9a3f41f1c41 to your computer and use it in GitHub Desktop.
gimp script to batch invert
#!/usr/bin/env python
#
# -------------------------------------------------------------------------------------
#
# Copyright (c) 2013, Jose F. Maldonado
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# - Neither the name of the author nor the names of its contributors may be used
# to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
import os
from gimpfu import *
def batch_invert(img, layer, inputFolder, outputFolder):
''' Inverts the colors of the PNG and JPEG images of a folder.
Parameters:
img : image The current image (unused).
layer : layer The layer of the image that is selected (unused).
inputFolder : string The folder of the images that must be inverted.
outputFolder : string The folder in which save the inverted images.
'''
# Iterate the folder
for file in os.listdir(inputFolder):
try:
# Build the full file paths.
inputPath = inputFolder + "\\" + file
outputPath = outputFolder + "\\" + file
# Open the file if is a JPEG or PNG image.
image = None
if(file.lower().endswith(('.png'))):
image = pdb.file_png_load(inputPath, inputPath)
if(file.lower().endswith(('.jpeg', '.jpg'))):
image = pdb.file_jpeg_load(inputPath, inputPath)
# Verify if the file is an image.
if(image != None):
# Invert the image.
if(len(image.layers) > 0):
layer = image.layers[0]
pdb.gimp_invert(layer)
# Save the image.
if(file.lower().endswith(('.png'))):
pdb.file_png_save(image, image.layers[0], outputPath, outputPath, 0, 9, 0, 0, 0, 0, 0)
if(file.lower().endswith(('.jpeg', '.jpg'))):
pdb.file_jpeg_save(image, layer, outputPath, outputPath, 0.9, 0, 0, 0, "Creating with GIMP", 0, 0, 0, 0)
except Exception as err:
gimp.message("Unexpected error: " + str(err))
register(
"python_fu_test_batch_invert",
"Batch invert",
"Inverts the colors of the PNG and JPEG images of a folder",
"JFM",
"Open source (BSD 3-clause license)",
"2013",
"<Image>/Filters/Test/Batch invert",
"*",
[
(PF_DIRNAME, "inputFolder", "Input directory", ""),
(PF_DIRNAME, "outputFolder", "Output directory", "")
],
[],
batch_invert)
main()
@HerbFargus
Copy link
Author

Put the .py file into the plugins folder (on windows for me it's C:\Users\USERNAME.gimp-2.8\plugins)

Then under the filters I have it under Test>batch invert then you should be able to go from there.

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