START
ESCWYP
MUSIC
VIDEOS
This is a Python-Fu extension/script for GIMP to scale JPEG images in batch mode from the command line.
The script expects a path to a directory where the images are located.
When for example your images are in /tmp/images/ and you put the script gimpbatchfuncs.py below into a file
then you can call from the directory containing gimpbatchfuncs.py
> gimp -idf --batch-interpreter=python-fu-eval -b 'import sys; sys.path=["."]+sys.path;import gimpbatchfuncs; gimpbatchfuncs.scale_images_dir("/tmp/images/")'
Every JPEG image in that directory will be scaled and saved with a new name, which is
the old name appended with a 'g' just before the file extension. So abc.jpg becomes abcg.jpg and so on.
To summerize:
1. Put your images in a directory, say /tmp/images
2. Put gimpbatchfuncs.py somewhere and from there call the command above with "/tmp/images/"
as parameter to gimpbatchfuncs.scale_images_dir()
The command explained:
gimp -idf --batch-interpreter=python-fu-eval # To run gimp in Python-Fu mode
-b 'COMMANDS' # What follows -b are the COMMANDS (in single quotes) to run
# which are:
import sys; # import sys
sys.path=["."]+sys.path; # add the current directory to the search path
import gimpbatchfuncs; # so we can import our script (note it's without the .py ending)
gimpbatchfuncs.scale_images_dir("/tmp/images/")' # and call the scale function with the image directory as parameter
In the gimpbatchfuncs.py script there are two variables to control how the images are scaled:
final_size # Gives the size (height or widht, which is controlled by fianl_size_is_height) of the scaled image
fianl_size_is_height # If set to True, final_size is the height of the scaled image
# If set to False, final_size is the width of the scaled image
So, if you want images to be 200 pixels in width. Then you set
final_size = 200
final_size_is_height = False
------------------------------------------------- gimpbatchfuncs.py -----------------------
#!/usr/bin/python
import os
from gimpfu import *
# size in pixels of the scaled image
final_size = 800
# if set to True, final_size is the height of the new image
# if set to False, final_size is the width of the new image
final_size_is_height = True
# scale one jpg image and save it.
def scale_one_image(path):
image = pdb.file_jpeg_load(path, "")
drawable = pdb.gimp_image_get_active_drawable(image)
if final_size_is_height:
f=1.0*drawable.height/final_size
nw=int(round(drawable.width/f))
nh=int(final_size)
else:
f=1.0*drawable.width/final_size
nw=int(final_size)
nh=int(round(drawable.height/f))
pdb.gimp_image_scale(image, nw, nh)
nn=os.path.splitext(path)[0]+"g.jpg"
pdb.file_jpeg_save(image, drawable, nn, "",0.95, 0, 0,0, "", 0, 1, 0, 0)
# scale only one jpg image, with hard-coded path. for testing.
def test():
scale_one_image("/tmp/test.jpg")
pdb.gimp_quit(TRUE)
# scale all jpg images in directory 'path'. Scaled images will be saved there with 'g' appended to their name.
def scale_images_dir(path):
if not os.path.isdir(path):
print("Argument", path, "is not a path. Exiting.")
pdb.gimp_quit(TRUE)
return
path = os.path.abspath(path)
files = os.listdir(path)
while len(files) > 0:
scale_one_image(path+"/"+files.pop())
pdb.gimp_quit(TRUE)
START
ESCWYP
MUSIC
VIDEOS