Skip to content

Instantly share code, notes, and snippets.

@bademux
Last active August 29, 2015 14:20
Show Gist options
  • Save bademux/f9bfd5f63958489dcd2a to your computer and use it in GitHub Desktop.
Save bademux/f9bfd5f63958489dcd2a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# License: MIT
# copy file to ~/.gimp-2.8/plug-ins and make it executeble, then run gimp with comdline
# gimp -ib '(python-wim RUN-NONINTERACTIVE "mytemplate.xcf" "guest_list.txt" "outdir")''(gimp-quit 0)'
import os, re, glob
from gimpfu import *
from gimpenums import *
# our script
def make_invitations(template, guest_list, outputdir) :
if not outputdir:
outputdir = os.path.dirname(guest_list)
pdb.gimp_message("Making Invitation using template[" + template + "] and guests list[" + guest_list + "]")
guests = open(guest_list).read().splitlines();
image = pdb.gimp_file_load(template, template)
tpl_layers = get_tpl_layers(image)
tpl_count = len(tpl_layers)
if tpl_count < 1:
pdb.gimp_message("Can't found Text Layer with name GuestName... in template " + template)
return
for guests_chunk in chunker(guests, tpl_count):
curr_image = pdb.gimp_image_duplicate(image)
for guest, tpl_layer in zip(guests_chunk, get_tpl_layers(curr_image)):
pdb.gimp_text_layer_set_text(tpl_layer, guest)
imagefile = outputdir + os.sep + "inv_" + guests_chunk[0] + ("" if tpl_count == 1 else "-" + guests_chunk[-1]) + ".png"
pdb.gimp_message(imagefile)
pdb.gimp_image_merge_visible_layers(curr_image, CLIP_TO_IMAGE)
#pdb.gimp_file_save(curr_image, pdb.gimp_image_get_active_layer(curr_image) imagefile, imagefile)
pdb.file_png_save2(curr_image, pdb.gimp_image_get_active_layer(curr_image), imagefile, imagefile, 0, 9, 0, 0 , 0, 0, 0, 0, 1)
pdb.gimp_image_delete(curr_image)
pdb.gimp_image_delete(image)
return
def chunker(seq, size):
return (seq[pos:pos + size] for pos in xrange(0, len(seq), size))
def get_tpl_layers(image):
container=[]
for layer in image.layers:
if pdb.gimp_item_is_group(layer):
container.extend( get_tpl_layers(layer) )
if layer.name.lower().startswith( 'guestname' ) and pdb.gimp_drawable_is_text_layer(layer):
container.append(layer)
return container
# This is the plugin registration function
register(
"python_wim",
"WeddingInvitationMaker",
"Use template for making WeddingInvitation",
"bademux",
"",
"2015",
"<Toolbox>/Wedding/Wedding Invitation",
"",
[
(PF_FILENAME, "template_file", "Tempate File", "invitation.xcf"),
(PF_FILENAME, "guest_list_file", "Guest List (new line separated)", "guest_list.txt"),
(PF_DIRNAME, "output_directory", "0utputdir", ""),
],
[],
make_invitations,
)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment