Last active
August 29, 2015 14:20
-
-
Save bademux/5f3056f2764d97e27a08 to your computer and use it in GitHub Desktop.
quick and dirty image merger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# License: MIT | |
#quick and dirty image merger | |
#gimp -ib '(python-copy2a4 RUN-NONINTERACTIVE "~/pics" "inv_*.png" "~/pics")''(gimp-quit 0)' | |
import os, re, glob | |
from gimpfu import * | |
from gimpenums import * | |
# our script | |
def copy2a4(in_dir, glob_pattern, out_dir) : | |
if not out_dir: | |
out_dir = in_dir | |
pdb.gimp_message("Globbing: " + in_dir + os.sep + glob_pattern) | |
glob_result = pdb.file_glob(in_dir + os.sep + glob_pattern, 1) | |
if glob_result[0] == 0: | |
pdb.gimp_message("No files found in " + in_dir) | |
return | |
pdb.gimp_message("Make A4 with in_dir[" + in_dir + "] and out_dir[" + out_dir + "]") | |
curr_height = 0 | |
in_files = sorted(glob_result[1]); | |
for curr_file in in_files: | |
in_image = pdb.gimp_file_load(curr_file, curr_file) | |
curr_height_tmp = curr_height + in_image.height | |
if curr_height_tmp > 3508: | |
save_image(out_image) | |
pdb.gimp_image_delete(out_image) | |
curr_height = 0 | |
if curr_height == 0: | |
out_image = create_blank_image() | |
out_image.filename = out_dir + os.sep + "out_" + os.path.basename(in_image.filename) + ".png" | |
in_layer = pdb.gimp_layer_new_from_visible(in_image, out_image, os.path.basename(in_image.filename)) | |
out_image.add_layer(in_layer, 0) | |
in_layer.set_offsets(0, curr_height) | |
pdb.gimp_message("in_image.filename = " + os.path.basename(in_image.filename)) | |
pdb.gimp_message("curr_height = " + str(curr_height)) | |
curr_height = curr_height_tmp | |
if in_files[-1] == curr_file: | |
save_image(out_image) | |
pdb.gimp_image_delete(in_image) | |
return | |
def save_image(image): | |
pdb.gimp_image_merge_visible_layers(image, CLIP_TO_IMAGE) | |
pdb.file_png_save2(image, image.active_layer, image.filename, image.filename, 0, 9, 0, 0 , 0, 0, 0, 0, 1) | |
return | |
def create_blank_image(): | |
out_image = gimp.Image(2480, 3508, RGB); | |
#set background to black | |
pdb.gimp_context_set_background((255,255,255)) | |
#add layer with 100% of opacity | |
layer=pdb.gimp_layer_new(out_image, out_image.width, out_image.height, RGB_IMAGE, "base", 100, NORMAL_MODE) | |
pdb.gimp_image_add_layer(out_image, layer, 0) | |
#we need it with alpha channel | |
pdb.gimp_layer_add_alpha(layer) | |
#access its drawable | |
drw = pdb.gimp_image_active_drawable(out_image) | |
#fill the background - black | |
pdb.gimp_drawable_fill(drw, BACKGROUND_FILL) | |
return out_image | |
# This is the plugin registration function | |
register( | |
"python_copy2a4", | |
"Copy to A4 300dpi", | |
"Copy to A4 300dpi (2480x3508px)", | |
"bademux", | |
"", | |
"2015", | |
"<Toolbox>/Wedding/Copy to A4", | |
"", | |
[ | |
(PF_DIRNAME, "in_file", "Input Dir", ""), | |
(PF_STRING, "glob_pattern", "glob pattern", "inv_*.png"), | |
(PF_FILENAME, "out_dir", "OutputFile", ""), | |
], | |
[], | |
copy2a4, | |
) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment