GIMP using command line

9,766

Solution 1

  1. Open an image is really easy (image.png is the image that you want to open)

    gimp image.png
    

  1. Convert RGB image to Grayscale :

    Create a GIMP Script-Fu file (named here dmmConvertPNGtoGrayscale.scm and saved in $HOME/.gimp-2.8/scripts) :

    ; dmmPNGtoGrayscale - GIMP Script-Fu to convert a PNG image to Grayscale
    ;    This Script-Fu must be put in The GIMP's script directory
    ;    (e.g., $HOME/.gimp-1.2/scripts).
    ;    For command-line invocation, use the shell script rgbtogs.sh
    ;    For interactive invocation, run The GIMP and go to
    ;    Xtns -> Script-Fu -> dmm
    ;
    (define (dmmPNGtoGrayscale infile outfile)
       (let* ((image (car (file-png-load 1 infile infile)))
                 (drawable (car (gimp-image-active-drawable image)))
              )
    
             (gimp-convert-grayscale image)
    
             (file-png-save 1 image drawable outfile outfile 
                  1 0 0 0 0 0 0 )
                ; 1 Adam7 interlacing?
                ;   0 deflate compression factor (0-9)
                ;     0 Write bKGD chunk?
                ;       0 Write gAMMA chunk?
                ;         0 Write oFFs chunk?
                ;           0 Write tIME chunk?    ?? backwards in DB Browser
                ;             0 Write pHYS chunk?  ?? backwards in DB Browser
       )
    )
    
    (script-fu-register                                 ; I always forget these ...
       "dmmPNGtoGrayscale"                              ; script name to register
       "<Toolbox>/Xtns/Script-Fu/dmm/dmmPNGtoGrayscale" ; where it goes
       "dmm PNG (RGB or Indexed) to Grayscale"          ; script description
       "David M. MacMillan"                             ; author
       "Copyright 2004 by David M. MacMillan; GNU GPL"  ; copyright
       "2004-02-08"                                     ; date
       ""                                               ; type of image
       SF-FILENAME "Infile" "infile.png"                ; default parameters
       SF-FILENAME "Outfile" "outfile.png"
    )
    

    And launch it using this script (I named it rgbtogs.sh for example) :

    # rgbtogs.sh
    # Invoke The GIMP with Script-Fu dmmPNGtoGrayscale.scm
    # No error checking.
    
    if [ -e $1 ] 
    then
       echo "Usage: rgbtogs.sh degrees filebasename"
       echo "Error: Parameter (filename base) required"
       exit 1
    fi
    
    gimp -c -i -d -b "(dmmPNGtoGrayscale \"$1.png\" \"$1-gray.png\")" "(gimp-quit 0)"
    

    Give execution to the script and start it :

    chmod +x rgbtogs.sh
    ./rgbtogs.sh image
    

  1. Convert a PNG image to JPEG (or JPG) :

    Create a GIMP Script-Fu file (named here dmmConvertPNGtoJPG.scm and saved in $HOME/.gimp-2.8/scripts) :

    ; dmmConvertPNGtoJPG.scm - GIMP Script-Fu to Convert PNG to JPG
    ;    This Script-Fu must be put in The GIMP's script directory
    ;    (e.g., $HOME/.gimp-1.2/scripts).
    ;    For command-line invocation, use the shell script pngtojpg.sh
    ;    For interactive invocation, run The GIMP and go to
    ;    Xtns -> Script-Fu -> dmm
    ;
    (define (dmmConvertPNGtoJPG infile outfile)
       (let* ((image (car (file-png-load 1 infile infile)))
              (drawable (car (gimp-image-active-drawable image)))
             )
    
             (file-jpeg-save 1 image drawable outfile outfile 
                  0.75 0 1 1 "GIMP" 0 1 0 0 )
                ; 0.75 quality (float 0 <= x <= 1)
                ;      0 smoothing factor (0 <= x <= 1)
                ;        1 optimization of entropy encoding parameter (0/1)
                ;          1 enable progressive jpeg image loading (0/1)
                ;            "xxxx"  image comment
                ;                   0 subsampling option number
                ;                     1 force creation of a baseline JPEG
                ;                       0 frequency of restart markers 
                ;                         in rows, 0 = no restart markers
                ;                         0 DCT algoritm to use 
       )
    )
    
    (script-fu-register                                 ; I always forget these ...
       "dmmConvertPNGtoJPG"                             ; script name to register
       "<Toolbox>/Xtns/Script-Fu/dmm/dmmConvertPNGtoJPG" ; where it goes
       "dmm Convert PNG to JPG"                         ; script description
       "David M. MacMillan"                             ; author
       "Copyright 2004 by David M. MacMillan; GNU GPL"  ; copyright
       "2004-01-27"                                     ; date
       ""                                               ; type of image
       SF-FILENAME "Infile" "infile.png"                ; default parameters
       SF-FILENAME "Outfile" "outfile.png"
    )
    

    And launch it using this script (I named it pngtojpg.sh for example) :

    # pngtojpg.sh
    # Invoke The GIMP with Script-Fu dmmConvertPNGtoJPG.scm
    # No error checking.
    
    if [ -e $1 ] 
    then
       echo "Usage: pngtojpg.sh filebasename"
       echo "Error: Parameter 1 (filename base) required"
       exit 1
    fi
    
    gimp -c -i -d -b "(dmmConvertPNGtoJPG \"$1.png\" \"$1.jpg\")" "(gimp-quit 0)"
    

    Give execution to the script and start it :

    chmod +x pngtojpg.sh
    ./pngtojpg.sh image
    

Source : http://beefchunk.com/documentation/lang/gimp/GIMP-Scripts-Fu.html

NB : inside GIMP Script-Fu file, you can remove all lines which begins with ; character (or everything on the right on this character), these are just comments

NB: These .scm script-fu were made originally for GIMP 1.2, but I tested with GIMP 2.8 and there is no issue

Solution 2

I don't know how to use Gimp for this task, but actually I think the tools from the package imagemagick are better choice for such CLI tasks. This package is widely used as conversion tool on the web server's applications as MediaWiki and WordPress, also it is a back end for some operations performed by graphical apps such PhotoShop. First you need to install the package:

sudo apt install imagemagick

Then use the following command to accomplish the task (reference):

convert input-file.png -set colorspace Gray -separate -average output-file.jpg

If you need to convert all PNG files in the current directory you can use a loop like this:

for f in *.png; do convert "$f" -set colorspace Gray -separate -average "${f%.*}.jpg"; done
Share:
9,766
Sara
Author by

Sara

Updated on September 18, 2022

Comments

  • Sara
    Sara over 1 year

    I want to do some operation on some images using GIMP through the command line on Ubuntu. Operations that I want to do are:

    1. Open an image
    2. Convert an image to gray-scale
    3. Convert a PNG image to JPEG

    How I can do them using the command line?

  • damadam
    damadam over 4 years
    The best would be to add some explanations about what is done in these links
  • vanadium
    vanadium over 4 years
    Imagemagick is indeed by far easier, although if OP insists to use Gimp, it has a batchmode: gimp.org/tutorials/Basic_Batch
  • Sara
    Sara over 4 years
    I got this error when i run this code Usage: dmmPNGtoGrayscale.sh degrees filebasename Error: Parameter (filename base) required
  • damadam
    damadam over 4 years
    probably I made a mistake, starting the script wuold be ./rgbtogs.sh image where image is your image filename without extension
  • Sara
    Sara over 4 years
    it works but I still got an error : batch command experienced an execution error: Error: eval: unbound variable: dmmPNGtoGrayscale
  • damadam
    damadam over 4 years
    scm script-fu files must be placed inside hidden GIMP folder, so it's inside $HOME/.gimp2.8/scripts (these were made with GIMP 1.2, but I tested with 2.8 and it works)
  • xenoid
    xenoid over 4 years
    +1 For IM, but it's not used by Gimp (and certainly not by PS...)
  • pa4080
    pa4080 over 4 years
    Hi, @xenoid, thanks! I'm not sure about Gimp, but Photoshop is equipped with the IM's convert tool.
  • Sara
    Sara over 4 years
    Thank you so much for your help
  • Michael Schumacher
    Michael Schumacher over 4 years
    What's up with the "?? backwards in DB Browser" comments?
  • Manu
    Manu over 3 years
    Is there a way to call the function defined in the .scm file while remaining "stateless", i.e. not having to place the file in GIMP's config directory? Load the contents of the file just for the duration of the command?
  • damadam
    damadam over 3 years
    @Manu you need to change Gimp script directory to do that, but these scripts are used only if you use the right commands (bash files in my answer); I can't help you to do that unfortunately