|
Greetings
I have written a script that iterates through a directory of jpgs and adds a layer to it with a watermark. The script sleeps for 15 seconds on each iteration to allow the user to tweak the location of the wm. After the sleep I save the image as xcf and export it as png after flattening. The png looks great. The xcf is saved with the wm layer unmoved and in the original position that the script placed it. I have no idea what is happening here.
Any help is greatly appreciated.
Environment: Linux mint 18.3 on athlon 64bit Gimp 2.10.6 from flatpak Python 2.7.12
Script:
#!/usr/bin/env python
from gimpfu import * import glob import time import os
def add_watermark_layer_batch(wm_file, source_folder, save_folder, export_folder): # function code goes here... #open a watermark image pdb.gimp_message( "processing source folder " + source_folder)
(wm_dir, wm_basename) = os.path.split(wm_file) pdb.gimp_message("using watermark " + wm_file) wmf_image = pdb.gimp_file_load(wm_file, os.path.basename(wm_file))
#calulate ratio of wm height to width ratio = float(wmf_image.height) / float(wmf_image.width)
for fq_filename in glob.glob(os.path.join(source_folder + "/*.*") ):
(filepath, filename) = os.path.split(fq_filename)
#load the file from disk image = pdb.gimp_file_load( fq_filename, filename ) img_height = image.height
#add the wm layer to the image wmf_layer = pdb.gimp_layer_new_from_visible(wmf_image, image, "wmlayer") pdb.gimp_image_insert_layer(image, wmf_layer, None, 0)
# resize to 20% of image width img_width = image.width wm_width = img_width * .20 wm_height = round(wm_width * ratio)
wm_layer = pdb.gimp_image_get_layer_by_name(image, "wmlayer") # rescale layer pdb.gimp_layer_scale(wm_layer, wm_width, wm_height, True) #set opacity pdb.gimp_layer_set_opacity(wm_layer, 40 ) #get a display so we can edit the image display = pdb.gimp_display_new(image)
#build the save file name (fname, fext) = os.path.splitext(filename) save_filename = fname + "_wm.xcf" save_path = os.path.join(save_folder, save_filename)
#save the image as xcf drawable = pdb.gimp_image_get_active_drawable(image) pdb.gimp_xcf_save(0,image, drawable, save_path, save_filename)
#wait a bit to allow editing time.sleep(15) #build the export file name export_filename = filename + "_wm.png" export_path = os.path.join(export_folder, export_filename) onelayer = pdb.gimp_image_merge_visible_layers(image, CLIP_TO_IMAGE)
#export as png pdb.file_png_save(image, onelayer, export_path, export_filename, 0, 9, 1, 0, 0, 0, 0) pdb.gimp_display_delete(display) # pdb.gimp_image_delete(image) time.sleep(.5) pdb.gimp_image_delete(wmf_image)
register( "python-fu-add-watermark-layer-batch", "Add a layer with a watermark", "Select a directory and load all images one by one, add a watermark layer and set the transparency to 50% and export a png file", "Muddy Waters", "Muddy Waters", "2018", "Batch add watermark layer", "", # type of image it works on (*, RGB, RGB*, RGBA, GRAY etc...) [ # (PF_DIRNAME, "sourceFolder", "Source directory", "/home/bret/bigdrive/play/"), # (PF_DIRNAME, "save_folder", "Save directory", "/home/bret/bigdrive/play/"), # (PF_DIRNAME, "export_folder", "Export directory", "/home/bret/bigdrive/play/")
(PF_FILE, "wm_image", "Watermark image", "/tmp/test/watermark/WatermarkCropped_826_wide.xcf"), (PF_DIRNAME, "source_folder", "Source directory", "/tmp/test"), (PF_DIRNAME, "save_folder", "Save directory", "/tmp/test/xcfs"), (PF_DIRNAME, "export_folder", "Export directory", "/tmp/test/finals") ], [], add_watermark_layer_batch, menu="<Image>/File") # second item is menu location
main()
|