Skip to content

Instantly share code, notes, and snippets.

@eyecreate
Last active December 19, 2016 19:51
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save eyecreate/5369685 to your computer and use it in GitHub Desktop.
Quick modification of find replace to allow multiple replacement at once with comma separated lists from a spreadsheet/csv.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Chris Mohler
# Multi-replace: Eyecreate
# Copyright 2008 Chris Mohler
# License: GPL v3
# GIMP plugin to perform search and replace on all text layers
from gimpfu import *
import re
gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
def find_and_replace(image, drawable, find, replace, all, case):
layers = image.layers #get image layers
if all: count = 0 #replace all
else: count = 1 #replace only first match
splitFind = find.split(',')
splitReplace = replace.split(',')
splitReplaceCount = 0
for element in splitFind:
if not case: element = re.compile(element, re.I) #case-insentive
for layer in layers:
if pdb.gimp_drawable_is_text_layer(layer): #only act on text layer
haystack = pdb.gimp_text_layer_get_text(layer) # get texr
if haystack == element: #prevents replacing parts of strings
replaced = re.sub(element, splitReplace[splitReplaceCount], haystack, count) # replace text
pdb.gimp_text_layer_set_text(layer, str(replaced)) # set text
splitReplaceCount += 1
register(
"python-fu-text-find-and-replace",
"Find and Replace text in all text layers.",
"Find and Replace text in all text layers.",
"Chris Mohler",
"Chris Mohler",
"2008",
"<Image>/Edit/Find & Replace Text...",
"RGB*, GRAY*",
[
(PF_STRING, "find", "Find (comma seperated):", ""),
(PF_STRING, "replace", "Replace (comma seperated):", ""),
(PF_TOGGLE, "all", "Replace All?", True ),
(PF_TOGGLE, "case", "Case Sensitive?", True),
],
[],
find_and_replace,
domain=("gimp20-python", gimp.locale_directory))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment