Skip to content

Instantly share code, notes, and snippets.

@allanlw
Created June 4, 2016 14:11
Show Gist options
  • Save allanlw/9dd65c6e49f32ce84bd8f66f42a006e5 to your computer and use it in GitHub Desktop.
Save allanlw/9dd65c6e49f32ce84bd8f66f42a006e5 to your computer and use it in GitHub Desktop.
Gimp Magicwand cut script
# This file contains a function for cutting a subject out of an image.
# Right now it just uses the top left corner, and gimp's magic wand. Could do something.
import gimpfu
import os
# if debug==True, we place on a green background
def convert(filename, new_name, debug=False):
img = pdb.gimp_file_load(filename, filename)
layer = pdb.gimp_image_merge_visible_layers(img, 1)
pdb.gimp_context_set_antialias(True)
pdb.gimp_context_set_feather(True)
pdb.gimp_context_set_feather_radius(5, 5)
#pdb.gimp_context_set_sample_criterion
#pdb.gimp_context_set_sample_threshold
#pdb.gimp_context_set_sample_transparent
# 0 == CHANNEL_OP_ADD
pdb.gimp_image_select_contiguous_color(img, 0, layer, 0, 0)
if not pdb.gimp_edit_cut(layer):
print "Couldn't cut...?"
if debug:
# 1 == RGBA_IMAGE
# 0 == NORMAL_MODE
layer_bg = pdb.gimp_layer_new(img, pdb.gimp_image_width(img), pdb.gimp_image_height(img), 1, "bg", 100, 0)
pdb.gimp_image_insert_layer(img, layer_bg, None, 1) # insert to bottom
# BRIGHT FUCKING GREEN
pdb.gimp_context_set_foreground((0, 255, 0))
pdb.gimp_drawable_fill(layer_bg, 0) # 0 == FOREGROUND_FILL
pdb.gimp_layer_add_alpha(layer)
layer = pdb.gimp_image_merge_visible_layers(img, 1)
pdb.gimp_file_save(img, layer, new_name, new_name)
pdb.gimp_image_delete(img)
try:
convert(os.environ["PY_CUT_INPUT"], os.environ["PY_CUT_OUTPUT"], os.environ.has_key("PY_CUT_DEBUG"))
finally:
pdb.gimp_quit(1)
#!/bin/bash
# Shell script for calling cut_gimp.py sanely
# Takes two args, input and output
dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
export PY_CUT_INPUT="$1"
export PY_CUT_OUTPUT="$2"
gimp -n -i --batch-interpreter=python-fu-eval -b - < $dir/cut_gimp.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment