Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transform an image into different aspect ratios
#7
I want to add a white frame around the image and found this solution to add to the existing code:

Code:
import sys, os
from gimpfu import *


def SetAspectRatios (image):
   #Output path
   outputPath = "C:\\Users\\"  
    # create the object of the image
   
   #Aspec Ratio 3-4
   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)
   
   # rescale the layer
   pdb.gimp_layer_scale(new_layer, 4538, 6537, 1)
   # resize the layer
   pdb.gimp_layer_resize(new_layer, 6000, 8000, 731, 731)
   #add white background
   background=gimp.Layer(new_image,'Background',new_image.width,new_image.height,RGB_IMAGE,100.,LAYER_MODE_NORMAL)
   background.fill(FILL_WHITE)
   new_image.add_layer(background,len(new_image.layers))
   final_layer = pdb.gimp_image_merge_visible_layers(new_image, 1)

   img_name_ar_3_4 = outputPath + "AR_3-4_w_Border" + filename
   # define drawable
   # drw = new_image.layers[0]
   drw = final_layer
   # save the image as jpeg
   pdb.file_jpeg_save(new_image, drw, img_name_ar_3_4, img_name_ar_3_4, 0.90, 0, 1, 1, "Aspect Ratio 3-4", 3, 1, 0, 2)
   # delete the new image
   gimp.delete(new_image)

           
### Registration
whoiam="\n"+os.path.abspath(sys.argv[0])
author="K"
menu="<Image>/Kevin/SetAspectR/"
copyrightYear="2024"
desc="Set Aspect Ratios v.1.0"

register(
    "SetAspectRatios",
    desc+whoiam,desc,
    author,author,
    copyrightYear,
    "Set Aspect Ratios v.1.0.",
    "*",
    [
        (PF_IMAGE, "image", "Input image", None)          
    ],
    [],
    SetAspectRatios,
    menu=menu
)

main()

Is this the best way to do it or is there a more elegant version? It works but I think it can be done better.
Reply


Messages In This Thread
RE: Transform an image into different aspect ratios - by SpaceMonkey - 02-05-2024, 09:42 PM

Forum Jump: