Quick and Dirty

January 4, 2012

Batch editing images

Filed under: Uncategorized — GSM @ 4:25 pm

I had a bunch of multi-page pdf documents where each page contains an image in the upper-left corner. I needed to move the images closer to the center of the pages. To do this manually would be incredibly painful. Here’s what I did after some research.

First, convert a multi-page pdf into a series of jpg’s:

convert -density 300 abc.pdf abc.jpg

This will produce abc-0.jpg, abc-1.jpg, etc., one per page. Next comes the interesting part. Fire up gimp, select from menu Filters -> Python-Fu -> Console. Enter the following code.

import os
def move(glob, x, y):
  num_files, files = pdb.file_glob(glob, 1)
  for file in files:
    image = pdb.gimp_file_load(file, file)
    pdb.gimp_layer_translate(image.active_layer, x, y)
    drawable = pdb.gimp_image_flatten(image)
    new_file_name = os.path.join(os.path.dirname(file), 'modified-' + os.path.basename(file))
    gimp_file_save(image, drawable, new_file_name, new_file_name)

Then open one of the jpg’s. Manually move the image to the desired position, and note the offsets in the x and y direction. Next just call the move function with the filename pattern and the offsets. Voila, you have modified-abc-0.jpg, modified-abc-1.jpg, etc.

Finally, compiled the jpg’s back to a pdf:

convert modified-abc-*.jpg modified-abc.pdf

 

Powered by WordPress