GIMP using command line
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
Open an image is really easy (
image.png
is the image that you want to open)gimp image.png
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
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