Post Reply 

resize and add layer script

Apr 1, 2014, 09:07 (This post was last modified: Apr 1, 2014 09:08 by bulevardi.)
Post: #1
resize and add layer script
Hi all,

I'm currently new to The Gimp.
Just abandoned photoshop few weeks ago and starting to get my macros done in Gimp.
I chose for Python, but I want to try Scheme aswel if that's easier. I'm new to both.

It's not working, I probably made some major errors in the syntax, first of all, the menu is not even showing.

I need something that:
- checks wether the photo is horizontal/vertical/square
- resizes to a max length specified in the script
- opens image as layer on top, to add an image containing a border
- saves as jpg max quality + add '-border' in the name

Can someone help? Wether Python or Scheme...
I'm currently running GIMP in Windows, later on I'll use this in Linux too. But do I have to install Python separately? Maybe Scheme is easier?

I'm that far already:


Code:
#!/usr/bin/python

from gimpfu import *


def plugin_main(myImage, drawable):
    

    currentWidth = drawable.width
    currentHeight = drawable.height
    
     #check if it's a horizontal/vertical/square photo:

    if (currentWith > currentHeight):
    
    pdb.gimp_image_scale_full(myImage, 800, 533, INTERPOLATION_NONE)
    pdb.gimp_file_load_layer("/../horizontal.psd")

    elsif (currentWith < currentHeight):

    pdb.gimp_image_scale_full(myImage, 533, 800, INTERPOLATION_NONE)
    pdb.gimp_file_load_layer("/../vertical.psd")

    elsif (currentWith = currentHeight):

    pdb.gimp_image_scale_full(myImage, 532, 532, INTERPOLATION_NONE)
    pdb.gimp_file_load_layer("/../square.psd")

     # save


      pdb.file_jpeg_save(myImage, drawable, myImage.name+"-border.jpg", myImage.name+"-border.jpg", 1, 0, 0, 0, "", 0, 0, 0, 0)


register(
        "python_fu_myscript",
        "Saves the image with border",
        "Saves the image with border",
        "Dirk Desmet",
        "Dirk Desmet",
        "2014",
        "<Image>/MyScripts/addborder",
        "*, *",
        [],
        [],
        plugin_main)

main()

Thanks in advance!
Find all posts by this user
Quote this message in a reply
Apr 1, 2014, 09:47 (This post was last modified: Apr 1, 2014 09:48 by paynekj.)
Post: #2
RE: resize and add layer script
You have a number of Python errors in your code:

In Python indentation is significant.

elsif is not a key-word, you should use elif

You can't do assignments (=) in if/elif tests, you must use the test for equality instead (==)

So I got the following to register in the menus (I haven't checked if it actually works):
Code:
#!/usr/bin/python

from gimpfu import *


def plugin_main(myImage, drawable):
    

    currentWidth = drawable.width
    currentHeight = drawable.height
    
     #check if it's a horizontal/vertical/square photo:

    if (currentWith > currentHeight):
    
      pdb.gimp_image_scale_full(myImage, 800, 533, INTERPOLATION_NONE)
      pdb.gimp_file_load_layer("/../horizontal.psd")

    elif (currentWith < currentHeight):

      pdb.gimp_image_scale_full(myImage, 533, 800, INTERPOLATION_NONE)
      pdb.gimp_file_load_layer("/../vertical.psd")

    elif (currentWith == currentHeight):

      pdb.gimp_image_scale_full(myImage, 532, 532, INTERPOLATION_NONE)
      pdb.gimp_file_load_layer("/../square.psd")

     # save


    pdb.file_jpeg_save(myImage, drawable, myImage.name+"-border.jpg", myImage.name+"-border.jpg", 1, 0, 0, 0, "", 0, 0, 0, 0)


register(
        "python_fu_myscript",
        "Saves the image with border",
        "Saves the image with border",
        "Dirk Desmet",
        "Dirk Desmet",
        "2014",
        "<Image>/MyScripts/addborder",
        "*, *",
        [],
        [],
        plugin_main)

main()

Kevin
Find all posts by this user
Quote this message in a reply
Apr 1, 2014, 10:10 (This post was last modified: Apr 1, 2014 10:11 by bulevardi.)
Post: #3
RE: resize and add layer script
(Apr 1, 2014 09:47)paynekj Wrote:  elsif is not a key-word, you should use elif

Woah, thanks for the quick reply!

I saw in a tutorial before the "elif" keyword, and thought that it was a typo Big Grin But it really is elif Big Grin
Ok, got it Smile

Going to test this tonight on the computer where I have gimp installed. I'll let you know if it works Smile

I'm wondering if all parameters are needed in the save function for example:
pdb.file_jpeg_save(myImage, drawable, myImage.name+"-border.jpg", myImage.name+"-border.jpg", 1, 0, 0, 0, "", 0, 0, 0, 0)

And the INTERPOLATION_NONE in the image scale function won't be necessary I presume...

Thanks anyhow Smile
Find all posts by this user
Quote this message in a reply
Apr 1, 2014, 10:19
Post: #4
RE: resize and add layer script
(Apr 1, 2014 10:10)bulevardi Wrote:  I'm wondering if all parameters are needed in the save function for example:
pdb.file_jpeg_save(myImage, drawable, myImage.name+"-border.jpg", myImage.name+"-border.jpg", 1, 0, 0, 0, "", 0, 0, 0, 0)

And the INTERPOLATION_NONE in the image scale function won't be necessary I presume...

All the parameters are required and so must be present. GIMP functions don't have default values for parameters and they must be supplied.

Kevin
Find all posts by this user
Quote this message in a reply
Apr 1, 2014, 11:38
Post: #5
RE: resize and add layer script
(Apr 1, 2014 09:07)bulevardi Wrote:  Hi all,

I'm currently new to The Gimp.
Just abandoned photoshop few weeks ago and starting to get my macros done in Gimp.
I chose for Python, but I want to try Scheme aswel if that's easier. I'm new to both.

It's not working, I probably made some major errors in the syntax, first of all, the menu is not even showing.

I need something that:
- checks wether the photo is horizontal/vertical/square
- resizes to a max length specified in the script
- opens image as layer on top, to add an image containing a border
- saves as jpg max quality + add '-border' in the name

Can someone help? Wether Python or Scheme...
I'm currently running GIMP in Windows, later on I'll use this in Linux too. But do I have to install Python separately? Maybe Scheme is easier?

I'm that far already:


Code:
#!/usr/bin/python

from gimpfu import *


def plugin_main(myImage, drawable):
    

    currentWidth = drawable.width
    currentHeight = drawable.height
    
     #check if it's a horizontal/vertical/square photo:

    if (currentWith > currentHeight):
    
    pdb.gimp_image_scale_full(myImage, 800, 533, INTERPOLATION_NONE)
    pdb.gimp_file_load_layer("/../horizontal.psd")

    elsif (currentWith < currentHeight):

    pdb.gimp_image_scale_full(myImage, 533, 800, INTERPOLATION_NONE)
    pdb.gimp_file_load_layer("/../vertical.psd")

    elsif (currentWith = currentHeight):

    pdb.gimp_image_scale_full(myImage, 532, 532, INTERPOLATION_NONE)
    pdb.gimp_file_load_layer("/../square.psd")

     # save


      pdb.file_jpeg_save(myImage, drawable, myImage.name+"-border.jpg", myImage.name+"-border.jpg", 1, 0, 0, 0, "", 0, 0, 0, 0)


register(
        "python_fu_myscript",
        "Saves the image with border",
        "Saves the image with border",
        "Dirk Desmet",
        "Dirk Desmet",
        "2014",
        "<Image>/MyScripts/addborder",
        "*, *",
        [],
        [],
        plugin_main)

main()

Thanks in advance!

Python is likely to be easier to learn. It can do everything done in Scheme plus a lot more... In Windows it comes bundled with the Gimp 2.8 installers. On Linux it is part of the standard system installation (there are plenty of standard scripts written in Python).

For you code, a first list of remarks;
  • The python-fu prefix is unnecessary in he registration name, There is one added automatically. Note that this nale must be unique, so you have better use something more specific than "myscript" otherwise you next script won't work Smile
  • "*,*" should be "*"
  • Since you have no input parameter, your plugin_main() function cannot take any. Typically you have to declare at least an image, and very often and image and a drawable. Then these are set by default to the current image and drawable when you start the script.
  • "<Image>/MyScripts/addborder" should be "Add border", and you can add a last parameter: menu="<Image>/MyScripts"
  • Maybe not important right now, but if you want to frame the image you should use image.height and image.width and not the drawable dimensions (the drawable can be smaller than the image).
  • You are lucky if you can tell where "/../horizontal.psd" is. There are a bunch of global variables that point to various directories. gimp.directory points to the Gimp profile (a good place to store script-related files IMHO, and gimp.user_directory(directory#) returns various user directories (Desktop, Documents, Pictures, Downloads...).
  • I'm bot sure you want to scale the image (you will distort it). Maye you want to change the canvas size (and fill any gaps created) instead.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Apr 1, 2014, 20:29 (This post was last modified: Apr 1, 2014 20:31 by bulevardi.)
Post: #6
RE: resize and add layer script
He's still not doing it.

I made the plugin selectable in the menu already.
But than when clicking, it does not do anything.

Tried to change lots of things ... but when testing in the console, he's giving me indentation problems, while they should've been resolved.

Quote:#!/usr/bin/env python

from gimpfu import *



def plugin_main(my_image, drawable):
currentWidth = my_image.width
currentHeight = my_image.height


#check if it's a horizontal/vertical/square photo:

if (currentWith > currentHeight):

pdb.gimp_image_scale_full(my_image, 800, 533, INTERPOLATION_NONE)
pdb.gimp_file_load_layer("D:\Fotos\Border Flickr 800x532 Horizontaal.psd")

elif (currentWith < currentHeight):

pdb.gimp_image_scale_full(my_image, 533, 800, INTERPOLATION_NONE)
pdb.gimp_file_load_layer("D:\Fotos\Border Flickr 800x532 Vertikaal.psd")

elif (currentWith == currentHeight):

pdb.gimp_image_scale_full(my_image, 532, 532, INTERPOLATION_NONE)
pdb.gimp_file_load_layer("D:\Fotos\Border Flickr 800x532 Square.psd")

# save


pdb.file_jpeg_save(my_image, drawable, my_image.name+"-border.jpg", my_image.name+"-border.jpg", 1, 0, 0, 0, "", 0, 0, 0, 0)


register(
"Flickr",
"Saves the image with border",
"Saves the image with border",
"Dirk Desmet",
"Dirk Desmet",
"2014",
"<Image>/MyScripts/Test",
"*",
[],
[],
plugin_main,
)

main()

Perhaps I should try Scheme instead.
Find all posts by this user
Quote this message in a reply
Apr 1, 2014, 21:47
Post: #7
RE: resize and add layer script
In Scheme, it's working as it should !

Quote:(define (Flickr-Hori image drawable)
(let*(
(back-layer 0)
)

(gimp-image-scale image 800 533)

(set! back-layer(car (gimp-file-load-layer RUN-NONINTERACTIVE image "D:/Fotos/hori.png")))

(gimp-image-insert-layer image back-layer -1 0)
(gimp-displays-flush)

(file_jpeg_save(1, image, drawable, image.name+"-border.jpg", image.name+"-border.jpg", 1, 0, 0, 0, "", 0, 0, 0, 0))

)
)

Only the file save as jpg line is n't working
Find all posts by this user
Quote this message in a reply
Apr 1, 2014, 22:22
Post: #8
RE: resize and add layer script
Indentation problems are usually due to a mix of spaces and tabs (they look alike on the screen, but they are considered different). Befreo you run the script in Gimp, run it in a command prompt like this:
Code:
python thescript.py
("python" may have to be replace by a full path, such as "C:\Program files\Gimp\python\python.exe")

All syntax problems will show up then. When you get a message about Gimpfu not found, it means the interpreter find your code runnable (gimpfu is only available when running in Gimp).

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Apr 2, 2014, 06:01
Post: #9
RE: resize and add layer script
(Apr 1, 2014 21:47)bulevardi Wrote:  In Scheme, it's working as it should !

Quote:(define (Flickr-Hori image drawable)
(let*(
(back-layer 0)
)

(gimp-image-scale image 800 533)

(set! back-layer(car (gimp-file-load-layer RUN-NONINTERACTIVE image "D:/Fotos/hori.png")))

(gimp-image-insert-layer image back-layer -1 0)
(gimp-displays-flush)

(file_jpeg_save(1, image, drawable, image.name+"-border.jpg", image.name+"-border.jpg", 1, 0, 0, 0, "", 0, 0, 0, 0))

)
)

Only the file save as jpg line is n't working

You have to use string-append to join strings together not +

Kevin
Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Request for script chlebna 1 174 Sep 13, 2017 09:35
Last Post: ythgilb
  Request for script - Guillotine + Watermark sunslick 1 230 Sep 4, 2017 08:51
Last Post: ythgilb
  Script-fu: How to create new, addressable layer using gimp-floating-sel-to-layer Bertholom√§us 2 232 Aug 31, 2017 14:32
Last Post: Bertholomäus
  How to modify this script-fu? Zakary81 1 196 Aug 23, 2017 12:48
Last Post: paynekj
  Need help in running a gui script-fu via command line eyalzek 2 356 Oct 4, 2014 14:05
Last Post: eyalzek

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS