It is currently Tue Aug 25, 2026 2:33 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: image changed in script sleep not saving changes
PostPosted: Tue Sep 18, 2018 11:44 pm  (#1) 
Offline
New Member

Joined: Sep 12, 2018
Posts: 2
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()


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: image changed in script sleep not saving changes
PostPosted: Wed Sep 19, 2018 2:08 am  (#2) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4812
muddywaters wrote:
After the sleep I save the image as xcf
        #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"



No you don't. You do it before.

PS: Please use [code] tags

_________________
Image


Top
 Post subject: Re: image changed in script sleep not saving changes
PostPosted: Wed Sep 19, 2018 9:19 am  (#3) 
Offline
New Member

Joined: Sep 12, 2018
Posts: 2
Arrrgh. Unbelievable. Thank you.

I guess was so tickled to get the rest of my first attempt at python and gimp scripting working I guess I just had tunnel vision. It took way more time than I care to admit, but have learned a lot in these last few evenings

I wondered how people made code look like code but by the time I decide that this time I would actually ask rather than exercise my google-fu I just needed to get it out there.

I have a follow up about clean up at the end of each iteration of the loop:

      pdb.gimp_display_delete(display)
   #   pdb.gimp_image_delete(image)
      time.sleep(.5)



At some point I was getting intermittent errors and it felt like there was some sort of race condition so added the .5 sleep and I am pretty sure that issue went away. There was also a time that I ended up with the image delete throwing an error but that may have been due to my changing stuff on the fly trying to get something or other working. There really was a fair bit of stuff to learn to get this far.

What is the correct way to clean up after each iteration? Is the sleep needed? I'll play with it now that I know there is not something funky going on with the save other than a keyboard short. If it would be better to post this follow up in a separate thread I don't mind doing that. What are the norms on this list?


Thanks again,

MW


Top
 Post subject: Re: image changed in script sleep not saving changes
PostPosted: Wed Sep 19, 2018 3:33 pm  (#4) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4812
The doc of gimp_image_delete() says:
Quote:
If there are no displays associated with this image it will be deleted. This means that you can not delete an image through the PDB that was created by the user. If the associated display was however created through the PDB and you know the display ID, you may delete the display. Removal of the last associated display will then delete the image.


But better safe than sorry, if the image is already deleted nothing happens, so you can do it anyway just in case.
In practice my own code uses another call: "gimp.delete(image)". The sleep() is completely unnecessary.

_________________
Image


Top
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group