Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transform an image into different aspect ratios
#3
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! Smile
Reply


Messages In This Thread
RE: Transform an image into different aspect ratios - by SpaceMonkey - 01-22-2024, 07:32 PM

Forum Jump: