01-22-2024, 09:09 PM
(01-22-2024, 07:32 PM)SpaceMonkey Wrote: Thanks for the fast and detailed reply!
I implemented the points you mentioned.
Code:
#!/usr/bin/env python2
# Preferred form of import, avoids "gimpfu" prefixes all over.
from gimpfu import *
# The real code.
def setAspectRatios():
import os
# create the object of the image
image=gimp.image_list()[0]
filename = os.path.basename(image.filename)
# create a new image with the same dimensions
new_image = pdb.gimp_image_new(6000, 8000, 0)
# disable undo to make it faster
pdb.gimp_image_undo_disable(new_image)
# define layer to copy
current_layer = image.layers[0]
# create the new name AR 2-3
new_layer = pdb.gimp_layer_new_from_visible(image, new_image, current_layer)
# add new layer to the new image
pdb.gimp_image_add_layer(new_image, new_layer, 0)
# resize the layer
pdb.gimp_layer_resize(new_layer, 5333, 8000, -345, 0)
img_name_ar_2_3 = "AR_2-3_" + filename
# define drawable
drw = new_image.layers[0]
# save the image as jpeg
pdb.file_jpeg_save(new_image, drw, img_name_ar_2_3, img_name_ar_2_3, 0.90, 0, 1, 1, "Aspect Ratio 2-3", 3, 1, 0, 2)
# delete the new image
gimp.delete(new_image)
#Aspect Ratio 4-5
new_image = pdb.gimp_image_new(6000, 8000, 0)
pdb.gimp_image_undo_disable(new_image)
current_layer = image.layers[0]
new_layer = pdb.gimp_layer_new_from_visible(image, new_image, current_layer)
pdb.gimp_image_add_layer(new_image, new_layer, 0)
pdb.gimp_layer_resize(new_layer, 5333, 8000, -345, 0)
img_name_ar_4_5 = "AR_4-5_" + filename
drw = new_image.layers[0]
pdb.file_jpeg_save(new_image, drw, img_name_ar_4_5, img_name_ar_4_5, 0.90, 0, 1, 1, "Aspect Ratio 4-5", 3, 1, 0, 2)
gimp.delete(new_image)
register(
'set-aspect-ratios', # Unique ID, I prefix mine with "ofn-" to avoid clashes
'create different aspect ratios', # Description/title (short)
'Set Aspect Ratios', # Help (can be longer...)
"Author", # KJ
"Author", # Copyright KJ
"2024", # Copyright 2024
"Set Aspect Ratios", # The menu label
"RGB*", # The type of images it can work on. Use "*" for all types
# [ # List of input parameters
# (PF_IMAGE, "image", "Input image", None)
#
#],
[], # List of output parameters
setAspectRatios, # The Python code that implements the plugin
menu="<Image>/Filters", # Where the menu label above appears
)
main()
Sadly it wont register and show up in gimp - do you can see what I miss? I use gimp 2.10.32
Thanks in advance!
because you removed the thing that describes the input arguments by commenting in out, instead of making it an empty list. But it's better to keep it in an have an image argument to your plugin, so you can work even if several images are open in Gimp:
Code:
#!/usr/bin/env python2
# Preferred form of import, avoids "gimpfu" prefixes all over.
from gimpfu import *
# The real code.
def setAspectRatios(image):
import os
# create the object of the image
#image=gimp.image_list()[0] #### commented out, use the image in the argument instead
filename = os.path.basename(image.filename)
# create a new image with the same dimensions
new_image = pdb.gimp_image_new(6000, 8000, 0)
# disable undo to make it faster
pdb.gimp_image_undo_disable(new_image)
# define layer to copy
current_layer = image.layers[0]
# create the new name AR 2-3
new_layer = pdb.gimp_layer_new_from_visible(image, new_image, current_layer)
# add new layer to the new image
pdb.gimp_image_add_layer(new_image, new_layer, 0)
# resize the layer
pdb.gimp_layer_resize(new_layer, 5333, 8000, -345, 0)
img_name_ar_2_3 = "AR_2-3_" + filename
# define drawable
drw = new_image.layers[0]
# save the image as jpeg
pdb.file_jpeg_save(new_image, drw, img_name_ar_2_3, img_name_ar_2_3, 0.90, 0, 1, 1, "Aspect Ratio 2-3", 3, 1, 0, 2)
# delete the new image
gimp.delete(new_image)
#Aspect Ratio 4-5
new_image = pdb.gimp_image_new(6000, 8000, 0)
pdb.gimp_image_undo_disable(new_image)
current_layer = image.layers[0]
new_layer = pdb.gimp_layer_new_from_visible(image, new_image, current_layer)
pdb.gimp_image_add_layer(new_image, new_layer, 0)
pdb.gimp_layer_resize(new_layer, 5333, 8000, -345, 0)
img_name_ar_4_5 = "AR_4-5_" + filename
drw = new_image.layers[0]
pdb.file_jpeg_save(new_image, drw, img_name_ar_4_5, img_name_ar_4_5, 0.90, 0, 1, 1, "Aspect Ratio 4-5", 3, 1, 0, 2)
gimp.delete(new_image)
register(
'set-aspect-ratios', # Unique ID, I prefix mine with "ofn-" to avoid clashes
'create different aspect ratios', # Description/title (short)
'Set Aspect Ratios', # Help (can be longer...)
"Author", # KJ
"Author", # Copyright KJ
"2024", # Copyright 2024
"Set Aspect Ratios", # The menu label
"RGB*", # The type of images it can work on. Use "*" for all types
[ # List of input parameters
(PF_IMAGE, "image", "Input image", None) ####### reinstated the image input argument
],
[], # List of output parameters
setAspectRatios, # The Python code that implements the plugin
menu="<Image>/Filters", # Where the menu label above appears
)
main()
So, given this the code sorta works(*) but:
- you save the image with its initial extension (say, PNG) even if you enforce the jpeg format.
- image is saved in the "current directory" which is certainly not what you want in the general case. Once you have generated the new file name, you probably want to create a full path by putting it at the end of the directory of the source image (that you obtain with os.path.dirname()) using os.path.join().
(*) at least the script registers, doesn't crash, and produces output, but where and what are still open to debate.