Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Blending in Images into Mockups automatically
#6
Then some suggestions:

Use the new form of registration (menu entry and menu location in distinct parameters) and avoid the useless image/drawable parameters

Code:
def mockup_scaleandmove_horizontal():  # No arguments until you add useful ones such as inpout/output directories
    # Current code goes here

register(
   "python_fu_mockup_scaleandmove_horizontal",
   "Rescale, paint stroke, and save images",
   "Rescale the input image into 5 different sizes, paint a stroke around the scaled and moved original image, and save them with DPI set to 300.",
   "Daniel Kunz",
   "Daniel Kunz",
   "2024",
   "Mockup ScaleAndMove Horizontal",
   "",
   [],
   [],
   mockup_scaleandmove_horizontal,
   menu="<Image>/Filters/")

Avoid the "parallel arrays" "anti-pattern", use an array of tuples:

Code:
   mockups = [
       ("WoodFrame_1.jpg",     295,  611,  1461, 850),
       ("BlackBorder_1.jpg",   113,  236,  801,  540),
       ("Gaming_1.jpg",        212,  112,  492,  290),
       ("Gaming_2.jpg",        303,  171,  436,  270),
       ("LivingRoom_1.jpg"     163,  115,  699,  435),
       ]

Then iterate and unpack the mockups:

Code:
for index, mockup in enumerate(mockups):
    pdb.gimp_image_scale(original_image, scale_x[index], scale_y[index])
    pdb.gimp_layer_translate(outline_layer, pos_x[index], pos_y[index])

becomes:

Code:
for mockup, pos_x, pos_y, scale_x, scale_y in mockups:
    pdb.gimp_image_scale(original_image, scale_x, scale_y)
    pdb.gimp_layer_translate(outline_layer, pos_x, pos_y)

Instead of loading two separate images, and copying a layer from the second to the first, you can use pdb.gimp_file_load_layer(image, filename) to load a file directly as a layer (that you still need to insert at the right position).

Since your code changes the context (the whole of set_stroke_params() (which in passing you can call only once outside of the loops)), you can bracket the whole script between pdb.gimp-context-push() and pdb.gimp-context-pop() calls to restore the initial context when done.

Use os.path.splitext() to extract the root name of a file, and os.path.join() to create a full path from directories and file names.

Code:
output_foldername = "{}".format(filename_original[:-4])
is definitely the weirdest line of code, because it is just a very complicated way to do
Code:
output_foldername = filename_original[:-4]
or more properly (see above):
Code:
output_foldername = os.path.splitext(filename_original)[0]
Reply


Messages In This Thread
RE: Blending in Images into Mockups automatically - by Ofnuts - 03-29-2024, 08:20 AM

Forum Jump: