Skip to content

Instantly share code, notes, and snippets.

@ThomazPom
Last active September 1, 2022 20:07
Show Gist options
  • Save ThomazPom/6ad07b66bb66f3a4c28f0f6253cf8579 to your computer and use it in GitHub Desktop.
Save ThomazPom/6ad07b66bb66f3a4c28f0f6253cf8579 to your computer and use it in GitHub Desktop.
Execute this in gimp python fu console to remove black backgrounds from png images while keeping accurate colors
import glob, os
def smart_remove_bg(folder,rem_color="#000000"):
for filename in glob.glob(folder.replace('"','').strip()):
out_fname = os.path.join(os.path.dirname(filename),"out-"+os.path.basename(filename))
out_fname_xcf = os.path.join(os.path.dirname(filename),"xcf-"+os.path.basename(filename)+".xcf")
image = pdb.file_png_load(filename, filename)
drawable = pdb.gimp_image_get_active_drawable(image)
pdb.plug_in_colortoalpha(image, drawable, rem_color)
layer_copy = pdb.gimp_layer_new_from_drawable(drawable, image)
pdb.gimp_context_set_sample_criterion(10)
pdb.gimp_context_set_antialias(0)
pdb.gimp_context_set_feather(0)
pdb.gimp_context_set_sample_merged(0)
pdb.gimp_context_set_sample_transparent(0)
pdb.gimp_context_set_sample_threshold(255/255)
pdb.gimp_context_set_foreground(rem_color)
pdb.gimp_image_select_color(image, 2, drawable, "#FF0000") # Select whatever left with an insane threeshold
pdb.gimp_edit_fill(drawable, 0) # And fill it with the color we just removed
pdb.gimp_image_add_layer(image, layer_copy, 0)
end_layer = pdb.gimp_image_merge_visible_layers(image, 0)
pdb.file_png_save(image, end_layer, out_fname, filename, False, 8, False, False, False, False, False)
pdb.gimp_xcf_save(0, image, end_layer, out_fname_xcf, out_fname_xcf)
print "Done " + filename
smart_remove_bg( r"file_or_folder" )
@ThomazPom
Copy link
Author

Best docx to png here https://cloudconvert.com/docx-to-png

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