Skip to content

Instantly share code, notes, and snippets.

@city96
Created February 28, 2023 18:29
Show Gist options
  • Save city96/b65611ce14977b025b725cf60ce1e9cb to your computer and use it in GitHub Desktop.
Save city96/b65611ce14977b025b725cf60ce1e9cb to your computer and use it in GitHub Desktop.
GIMP Python-Fu script to create masks from OpenPose images.
# GIMP Python-Fu script to create (rough) masks from OpenPose images.
# Not recommended for singe images, mainly useful for animations/batches.
# Requires a uniform black background to work.
# Edit input/output folders, then paste into Filters->Python-Fu->Console.
import gimpcolor
import os
in_dir = r"O:\example\raw" # Input folder
out_dir = r"O:\example\out" # Output folder
grow_size = 48 # Main mask size
feather_size = 96 # Feather radius ("softness")
def mask(img,dw):
bg = gimpcolor.RGB(0,0,0)
mk = gimpcolor.RGB(255,255,255)
pdb.gimp_context_set_foreground(mk)
pdb.gimp_context_set_background(bg)
pdb.gimp_by_color_select(dw, bg, 32, 2, False, False, 0, False)
pdb.gimp_selection_invert(img)
pdb.gimp_selection_grow(img, grow_size)
if (feather_size > 0):
pdb.gimp_selection_feather(img, feather_size)
pdb.gimp_bucket_fill(dw, 0, 0, 100, 0, False, 0, 0)
def run():
for f in os.listdir(in_dir):
p = os.path.join(in_dir,f)
o = os.path.join(out_dir,f)
if p.endswith(".png"):
print(p)
i = pdb.file_png_load(p, p)
dw = pdb.gimp_image_active_drawable(i)
mask(i,dw)
pdb.file_png_save_defaults(i, dw, o, o)
run()
print("Complete")
@city96
Copy link
Author

city96 commented Feb 28, 2023

Example output:
inpaint

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