Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with small edit to a plug-in
#1
Question 
Hello! I do a lot of image work for my job and my little team really appreciates all the time and effort you all put into these plug-ins. Countless hours have been saved thanks to the work you all share and upload.
I was hoping I could find someone who could help write a small QOL edit a plug-in.

The plug-in is the Center Resize All Layers to Image Size created by Tin Tran over on http://gimplearn.net.

Would it be possible to have the code loop until it's Centered/Resized all layers and then delete the "Background" layer?


First I need to say: None of my team, myself included, is very good with GIMP. I'd still classify us all as novice, even after years. Some of them can barely understand what a "brush" or "layer" is and just follow a very basic step-by-step-with-pictures guide to get their basic edits done. I swear they're smart. Just... not with image work/programs.
We work with hundreds of images at a time. For the scaling/size, we typically open a new canvas with our desired size/dimensions, upload a ton of different sized images as layers, use that plug-in to center and resize all of the images to the canvas size, and then use another plug-in to export all layers as individual images. We export to a lot of different folders. Unfortunately, this means they also export the white Background layer to every folder as well. They will then go to every individual folder (sometimes a couple hundred) and delete the white background image.

It would be so much less of a headache to just have that layer deleted automatically before exporting.

Code:
#!/usr/bin/env python

# center_resize_all_layers_to_image_size.py
# Created by Tin Tran http://gimplearn.net
# License: GPLv3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# To view a copy of the GNU General Public License
# visit: http://www.gnu.org/licenses/gpl.html
#
#
# ------------
#| Change Log |
# ------------
# Rel 1: Initial release.
#import string
#import Image
import random
from gimpfu import *
import math
#from array import array
#import sys
    
def python_tt_center_resize_all_layers_to_image_size(image, layer):
    #convert to index mode
    # pdb.gimp_image_convert_indexed(image,CONVERT_DITHER_NONE,CONVERT_PALETTE_GENERATE,colors,FALSE,TRUE,'ignored')
    # num_bytes,colormap = pdb.gimp_image_get_colormap(image);
    # num_bytes_per_color = len(colormap)/colors
    # pdb.gimp_image_convert_rgb(image) #convert it back to RGB
    non_empty,x1,y1,x2,y2 = pdb.gimp_selection_bounds(image)
    if (non_empty == TRUE):
        width = x2-x1
        height = y2-y1
        for l in image.layers:
            pdb.gimp_layer_scale(l,width,height,TRUE)
            pdb.gimp_layer_set_offsets(l,x1,y1)
    else:    
        for l in image.layers:
            pdb.gimp_layer_scale(l,image.width,image.height,TRUE)
            pdb.gimp_layer_set_offsets(l,0,0)

register(
    "python_fu_tt_center_resize_all_layers_to_image_size",                          
    "Center Resize All Layers To Image Size",
    "Center Resize All Layers To Image Size",
    "Tin Tran",
    "Tin Tran",
    "August 2020",
    "<Image>/Python-Fu/Center Resize All Layers To Image Size",             #Menu path
    "RGB*, GRAY*",
    [
    # (PF_SPINNER, "width", "Width:", 2000, (10, 3000, 10)),
    # (PF_SPINNER, "height", "Height:", 2500, (10, 3000, 10)),
    ],
    [],
    python_tt_center_resize_all_layers_to_image_size)

main()
Reply
#2
Why not removing/deleting the background before exporting?

If it to resize and center a millions image's canvas, XnConvert is your friend and will go way faster than GIMP IMHO, drop your folders in XnConvert, setup your filters, output directories, and so... for an old slow computer like mine, it does > 5-600 images conversion per minute depending size, filters added... and so

(03-22-2022, 09:18 PM)Fizzgig Wrote: They will then go to every individual folder (sometimes a couple hundred) and delete the white background image.

I don't know if one will do the change in your python plugin, but you can do the removal of all the background via your operating system terminal as you are dealing with directories (folders). It will be done in less than a second by your OS.

Depending your directories set-up/trees and if name you give to the "background.jpg" is the same across all your directories (or has the same naming pattern).
On Linux I would go in the root directory of your saved work via terminal then run in the terminal something like this (That's just an example, and most important, I would some test on a dummy directory with sub-directories to be sure or fine tune it)
Code:
find . -name "background.jpg" -type f -delete

which basically will delete recursively all files named "background.jpg" in the directory you are in, as well as in all sub-directories.
For windows, I have no idea what the code will be, just wait, there are windows users here as well, or maybe some one will add the removal background line to your code. Wink
Reply
#3
Quote:Would it be possible to have the code loop until it's Centered/Resized all layers and then delete the "Background" layer?

Why not ask Tin Tran himself over at gimp-learn?
He wrote the plug-in, he knows best what it's about and how to extend it.
And he's a helping guy.
Reply
#4
@PixLab
Trying to install new software on our work PCs is a nightmare and a months long ordeal. If it's not deemed important enough by the IT department, it's a backlog issue that may honestly never actually get done.
I'm not sure most of my team knows enough about Windows to be able to reliably use the command prompt. They're good with Microsoft Office things and especially Excel, but we have a few stubborn older people who have a hard time learning new things on the PC. As you can imagine, converting to remote work was a rough time. They already know how to use these plug-ins for GIMP, it'd be easier (and less of a headache for me to explain) to just add it to something they already do.

But you raised a good point, I realize it would probably be easier to ask the author if it's possible to edit the export plug-in rather than edit in a loop/delete layer in this one.

@nelo
That's fair, I'll try to contact him. I assumed it'd be a simple edit and I would get a quicker response here. But I don't know much about programming, so my assumption just made an ass out of me.
Reply


Forum Jump: