03-19-2021, 01:13 AM
(03-18-2021, 05:40 PM)TimorousMe Wrote:(03-18-2021, 05:30 PM)Ofnuts Wrote: Btw, if you areusing the script only in batch, there is no need to register it as a plugin.
See https://stackoverflow.com/questions/4443...0#44435560
Thanks guys. I didn't realize you had responded and I fixed some things in my initial post (including the mismatch) and also tried a different command line call before seeing your responses.
I will remove the definition and also assign image as Kevin suggests and I'll report back.
I removed the definition part of my plugin as Ofnuts suggested, and then added the import language from the Stack Overflow post (as I cannot just call the function name now because it's not registered). I also followed Kevin's suggestion by assigning image = pdb.gimp_xcf_load(infileA, infileA) and then finding the active layer in image. The command line fails with the response below.
Plugin (renamed batch.py for simplicity)
Code:
#!/usr/bin/env python
import sys
from gimpfu import *
def open_add_flatten_export(infileA, infileB, x_offset, y_offset, outputFolder):
# Open XCF template
image = pdb.gimp_xcf_load(0, infileA, infileA)
layer = pdb.gimp_image_get_active_layer(image)
try:
# Open file.
fileImage = None
if(infileB.lower().endswith(('.jpeg', '.jpg'))):
fileImage = pdb.file_jpeg_load(infileB, infileB)
# Create new layer.
newLayer = gimp.Layer(image, "Added JPG", layer.width, layer.height, layer.type, layer.opacity, layer.mode)
# the +1 adds it behind the top layer
image.add_layer(newLayer, +1)
# Put image into the new layer.
fileLayer = fileImage.layers[0]
pdb.gimp_edit_copy(fileLayer)
floating = pdb.gimp_edit_paste(newLayer, True)
# Update the new layer.
newLayer.flush()
newLayer.merge_shadow(True)
newLayer.update(0, 0, newLayer.width, newLayer.height)
# Flatten + offset floating layer, then flatten image
pdb.gimp_floating_sel_to_layer(floating)
pdb.gimp_layer_set_offsets(floating, x_offset, y_offset)
pdb.gimp_image_flatten(image)
# Export JPG of flattened image
layer = pdb.gimp_image_get_active_layer(image)
pdb.file_jpeg_save(image, layer, outputFolder + "/" + "export.jpg", "raw_filename", 0.9, 0, 0, 0, "Creating with GIMP", 0, 0, 0, 0)
else:
gimp.message("The image could not be opened since it is not an image file.")
except Exception as err:
gimp.message("Unexpected error: " + str(err))
Command line command + arguments (modeled after Stack Overflow post)
Code:
gimp -idf --batch-interpreter python-fu-eval -b 'import sys;sys.path=['.']+sys.path;import batch;batch.run("/Users/TimB/Desktop/xcf_template.xcf" "/Users/TimB/Desktop/jpg_to_add.jpg" "2060" "410" "/Users/TimB/desktop")' -b 'pdb.gimp_quit(1)'
Response from command line:
Quote:GIMP is started as MacOS application
2021-03-18 21:10:18.605 gimp[2940:40051] *** WARNING: Method userSpaceScaleFactor in class NSView is deprecated on 10.7 and later. It should not be used in new applications. Use convertRectToBacking: instead.
/Applications/GIMP-2.10.app/Contents/MacOS/gimp: LibGimpBase-WARNING: gimp: gimp_wire_read(): error
gimp_check_updates_callback: loading of https://www.gimp.org/gimp_versions.json failed: Operation not supported
Traceback (most recent call last):
File "/Applications/GIMP-2.10.app/Contents/Resources/lib/gimp/2.0/python/gimpfu.py", line 827, in _run
return apply(func, params[1:])
File "/Applications/GIMP-2.10.app/Contents/Resources/lib/gimp/2.0/plug-ins/python-eval.py", line 25, in code_eval
exec code in globals()
File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'run'
batch command experienced an execution error
GIMP-Error: Failed to save data:
You have a writable data folder configured (/Users/TimB/Library/Application Support/GIMP/2.10/gradients), but this folder does not exist. Please create the folder or fix your configuration in the Preferences dialog's 'Folders' section.