Skip to content

Instantly share code, notes, and snippets.

@japsuu
Created November 1, 2023 08:32
Show Gist options
  • Save japsuu/24fa19a0b4552742b67b145bcf86f797 to your computer and use it in GitHub Desktop.
Save japsuu/24fa19a0b4552742b67b145bcf86f797 to your computer and use it in GitHub Desktop.
Gimp-Fu -plugin pipeline to automatically create colored variant layers for each layer in the current image, and export them to target folder.
#!/usr/bin/env python
# Type: Python-Fu
# Title: Export layers to PNG files
# Description: Automatically export each layer in the current image as a separate PNG file.
# Author: Jasper Honkasalo
# Version: 2023.1
# Menu-Path: File/Export layers to PNG files
from gimpfu import *
import os
def export_layers_to_png(image, drawable, output_folder):
for layer in image.layers:
# Ensure the layer is visible
pdb.gimp_item_set_visible(layer, True)
# Create the filename using the layer name
filename = output_folder + os.path.sep + layer.name + '.png'
# Export the layer to a PNG file
pdb.file_png_save(image, layer, filename, filename, 0, 9, 0, 0, 0, 0, 0)
gimp.message("Exported layers to PNG files in " + output_folder)
register(
"export_layers_to_png",
"Export layers to PNG files",
"Export each layer in the current image as a separate PNG file.",
"Jasper Honkasalo",
"Jasper Honkasalo",
"2023",
"<Image>/File/Export layers to PNG files",
"*",
[
(PF_DIRNAME, "output_folder", "Output Folder", "")
],
[],
export_layers_to_png)
main()
#!/usr/bin/env python
# Type: Python-Fu
# Title: Create variant layers
# Description: Automatically creates colored variant layers for each layer in the current image.
# Author: Jasper Honkasalo
# Version: 2023.1
# Menu-Path: File/Create variant layers
from gimpfu import *
import os
import time
def create_variant_layers(image, drawable, in_colorize, in_colorize_luminosity_mult, in_variants, in_variant_colors):
variant_colors = []
if in_colorize:
variant_color_strings = in_variant_colors.split(",")
for variant_color_string in variant_color_strings:
variant_color = variant_color_string.split(".")
if len(variant_color) != 3:
gimp.message("Error: variant colors must be in the format RRR.GGG.BBB")
return
variant_colors.append((int(variant_color[0]), int(variant_color[1]), int(variant_color[2]), 255))
if len(variant_colors) != len(in_variants.split(",")):
gimp.message("Error: variant colors and variants count must match")
return
variants = in_variants.split(",")
image.undo_group_start()
try:
for layer_index, layer in enumerate(image.layers):
for variant_index, variant in enumerate(variants):
image.active_layer = layer
should_lower_count = variant_index + 1
layer_copy = layer.copy()
image.add_layer(layer_copy)
layer_copy.name = layer.name + "_" + variant
for i in range(should_lower_count):
image.lower_layer(layer_copy)
if in_colorize:
pdb.plug_in_colorify(image, layer_copy, variant_colors[variant_index], run_mode=RUN_NONINTERACTIVE)
# Get the width and height of the layer
width = layer_copy.width
height = layer_copy.height
# Iterate through the pixels
for x in range(width):
for y in range(height):
# Get the pixel color at the current position
pixel = pdb.gimp_drawable_get_pixel(layer_copy, x, y)
color_count, color = pixel
if color[3] == 0:
continue
new_r = min(color[0] * in_colorize_luminosity_mult, 255)
new_g = min(color[1] * in_colorize_luminosity_mult, 255)
new_b = min(color[2] * in_colorize_luminosity_mult, 255)
new_a = color[3]
new_color = (new_r, new_g, new_b, new_a)
# Set the modified pixel back to the layer
pdb.gimp_drawable_set_pixel(layer_copy, x, y, color_count, new_color)
for layer_index, layer in enumerate(image.layers):
image.active_layer = layer
# Toggle the layer's visibility to trigger a preview update
layer.visible = not layer.visible
time.sleep(0.1)
layer.visible = not layer.visible
except Exception as e:
image.undo_group_end()
gimp.displays_flush()
gimp.message("Error: " + str(e))
return
image.undo_group_end()
gimp.displays_flush()
gimp.message("Created variant layers")
register(
"create_variant_layers",
"Create variant layers",
"Create variant layers for each layer in the current image.",
"Jasper Honkasalo",
"Jasper Honkasalo",
"2023",
"<Image>/File/Create variant layers",
"*",
[
(PF_TOGGLE, 'in_colorize', 'Colorize the variants?', False),
(PF_FLOAT, 'in_colorize_luminosity_mult', 'Luminosity multiplier?', 2.0),
(PF_STRING, 'in_variants', 'Variants to create', "attack,buff,damaged"),
(PF_STRING, 'in_variant_colors', 'Colors of variants to create (RRR.GGG.BBB)', "255.255.255,000.255.000,255.000.000"),
],
[],
create_variant_layers)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment