Gimptalk - Premier Gimp Community: Run a script from a file in Linux - Gimptalk - Premier Gimp Community

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Run a script from a file in Linux

#1 User is offline   kostka 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 26-July 10

Posted 26 July 2010 - 04:20 PM

I have a script in a text file as:
(let* 

  ; define variables in here
  (

    ; runmode (0 = interactive, 1 = noninteractive)
    (runmode 1)

    ; load the image
    (image (car (gimp-file-load runmode "test.png"
      "test.png")))

    ; get its drawable
    (drawable (car (gimp-image-get-active-drawable 1)))

    ; dimensions
    (width (car (gimp-drawable-width drawable)))
    (height (car (gimp-drawable-height drawable)))

    ;
    (maxdimension 0)
    (borderpixels 0)
    (megapixels 0)
    (targetmegapixels 0.75)
    (ratio 0)

  )

  ; erase the middle black bar on split-screen ParaView plots
  ; you can take this out if it interferes
  (gimp-edit-bucket-fill drawable 1 0 100 0 FALSE (round (/ (- width 1) 2)) 0)

  ; autocrop
  (plug-in-autocrop runmode image drawable)

  ; get current dimensions
  (set! width (car (gimp-drawable-width drawable)))
  (set! height (car (gimp-drawable-height drawable)))

  ; add a border
  (set! maxdimension (max width height))
  (set! borderpixels (max 1 (round (* maxdimension 0.01))))
  (set! width (+ width borderpixels borderpixels))
  (set! height (+ height borderpixels borderpixels))
  (gimp-image-resize image width height borderpixels borderpixels)

  ; resize the layer to match the new image size
  (gimp-layer-resize-to-image-size (car (gimp-image-get-active-layer image)))

  ; get current dimensions and megapixels
  (set! megapixels (/ (* width height) 1000000))

  ; find scaling factor
  (set! ratio (sqrt (/ targetmegapixels megapixels)))

  ; scale the image (using cubic interpolation)
  (set! width (round (* width ratio)))
  (set! height (round (* height ratio)))
  (gimp-image-scale-full image width height 2)

  ; save it
  (gimp-file-save runmode image drawable
    "gimpout.png"
    "gimpout.png")

  ; delete it from memory
  (gimp-image-delete image)

)


which works if I enter this into the script-fu console. I want to run this in Linux using something like

gimp -s -i -d -b < scriptfile


so it runs the script directly from the command line. Is there any way to accomplish this? I know you can do things like

gimp -s -i -d -b '(gimp-file-load 1 "test.png" "test.png"' 'othercommands...' '(gimp-quit 0)'


but this doesn't seem to be working for a larger command like this embedded in a (let* ) block. I don't want to register the script, which would force me to register it on every computer I use.

Thanks in advance!
0

#2 User is offline   SamKook 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 26-July 10

Posted 26 July 2010 - 05:04 PM

In windows I use something like this:
gimp-2.6 -i -b - < scriptfile


so I'm guessing the only thing you need to add is the dash before the "<" to make it work.
0

#3 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 26 July 2010 - 06:33 PM

SamKook is correct. You could also use pipes:

cat scriptfile | gimp -i -f -b -


or a "HERE document":

gimp -i -f -b <

HERE documents are nice if you want to create a shell script which you can execute by itself.

#!/usr/bin/bash
gimp -i -f -b <

I would caution that your file needs to consist of only a single Script-fu block. Your script is fine because 'let' is treated as a single block. If you have more than one block to execute then you should place them within a '(begin ... )' block.

Also, if you want GIMP to close after running your script (and return you to the command line), your script will need to include a '(gimp-quit 0)' command. This command should be the last command in your "single block".
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.
0

#4 User is offline   kostka 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 26-July 10

Posted 26 July 2010 - 08:08 PM

SamKook said:

In windows I use something like this:
gimp-2.6 -i -b - < scriptfile


so I'm guessing the only thing you need to add is the dash before the "<" to make it work.


Hm... it's not working. Seems like it should.

Here's a really simple example. My script file is:
(let*

  ; this is a comment
  (
    (something 1)
    (nothing 0)
  )

  ; this exits
  (gimp-quit 0)

)

and I run either of:
gimp -i -b - < scriptfile
cat scriptfile | gimp -i -b -

and I get:
ERROR: end of file inside list
batch command: executed successfully.


However, it doesn't exit, so the command is not being executed successfully. If I simply do
gimp -i -b '(gimp-quit 0)'

everything works as expected.

Also of interest, if I change the script file to
; this exits
(gimp-quit 0)

it works.

Any ideas?

Right now I've written a script to strip out the comments and make it into a single line, which works. Quite frustrating.
0

#5 User is offline   SamKook 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 26-July 10

Posted 26 July 2010 - 08:32 PM

In my script, (gimp-quit 0) is outside of the let statement.

I have a function declared with a let in it, then I run the function and then I run gimp-quit.

You can see an example in my post right before yours.
0

#6 User is offline   kostka 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 26-July 10

Posted 26 July 2010 - 08:44 PM

SamKook said:

In my script, (gimp-quit 0) is outside of the let statement.

I have a function declared with a let in it, then I run the function and then I run gimp-quit.

You can see an example in my post right before yours.


Thanks for looking into this. I appreciate the help. If I change the script file to
(let*

  ; this is a comment
  (
    (something 1)
    (nothing 0)
  )

  ; stuff...
  (gimp-message "This line executed")

)

; this exits
(gimp-quit 0)

it still doesn't work (same error). Also, the gimp-message line isn't executed. Is this what you meant?
0

#7 User is offline   SamKook 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 26-July 10

Posted 26 July 2010 - 09:19 PM

Yes, that's what I meant. Your script works fine for me in windows, so this is something linux specific and I'm afraid I can't help you much there.

You could try to englobe the let and gimp-quit with a "(begin" and ")" or try the other options saulgoode mentionned.
0

#8 User is offline   kostka 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 26-July 10

Posted 26 July 2010 - 09:28 PM

SamKook said:

Yes, that's what I meant. Your script works fine for me in windows, so this is something linux specific and I'm afraid I can't help you much there.

You could try to englobe the let and gimp-quit with a "(begin" and ")" or try the other options saulgoode mentionned.


Ah, thanks very much for the followup! Must be something funny with this linux/gimp configuration. I'll just use the workaround for now.
0

#9 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 26 July 2010 - 10:54 PM

Sorry about misleading you on the single block issue. That must only be a problem if you are using the "-b" command line option (or maybe it is a problem that has been fixed in recent versions).

ERROR: end of file inside list


This error usually indicates unmatched parentheses in your script.
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.
0

#10 User is offline   kostka 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 26-July 10

Posted 26 July 2010 - 11:05 PM

saulgoode said:

Sorry about misleading you on the single block issue. That must only be a problem if you are using the "-b" command line option (or maybe it is a problem that has been fixed in recent versions).

ERROR: end of file inside list


This error usually indicates unmatched parentheses in your script.


No problem, and thanks for the help. My guess is that the interpreter thinks the block is over at the end of line, instead of the end of file, since putting everything on a single line works.

This is version v2.2. It may be fixed in more recent versions.
0

#11 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 26 July 2010 - 11:23 PM

kostka said:

No problem, and thanks for the help. My guess is that the interpreter thinks the block is over at the end of line, instead of the end of file, since putting everything on a single line works.

This is version v2.2. It may be fixed in more recent versions.

2.2? That is old. I don't recall line breaks causing problems back then, but comments most definitely would. I suspect your script would work fine with line breaks, as long as you removed the comments.
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic