Question about automating cropping process

(Chintan Gohel) #1

I’ve been using rawtherapee for a couple of months now, mainly for processing multiple photos in one go using the same edits - it’s a wonderful software and runs well on my surface 3

Recently I’ve been wanting to do something different and though rawtherapee works, I have to do it manually

This is the scenario -I’ve taken 900 photos for a timelapse. Now I want to crop the photos in a progressive manner: if photo no 1 is 38402160, I want the next photo to be 38242151, and the next to be 38082142 and so on until the desired crop is reached, eg: 19201080

I’ve been doing this manually where I open eah photo, edit the crop values and export, open the next photo, crop and export

Is there a way to automate this somehow? For example I have a separate program where I enter the starting crop value and final crop value and give it the photos to work through and I get the processed images which I can then work on further?

Hoping to receive a quick answer :slight_smile:


Hi @Chintan_Gohel This is not possible in RawTherapee, but you do not have to open each photo, just use the batch edit panel on the right hand side, making sure you have the correct image selected in the file browser.

(Chintan Gohel) #3

But I still would have to change the crop on each photo individually since every photo has a different crop

Though I don’t have any programming experience, can a small program be created? Something along the lines of:

For photos in this range, increase/decrease crop by 2 pixels for every successive photo

(Alberto) #4

you can definitely write a script for that. you would generate a partial pp3 with the desired crop settings and then combine it with the general one, using rawtherapee-cli. now I’m typing from the phone so I can’t show you some code, but I hope you get the idea.

(Chintan Gohel) #5

This is what I’m looking for actually, a kind of script - I would do this but I’ve never done programming :confused:

I would be grateful if you can help out - I can wait since this isn’t a one time thing but something I could use for many timelapses in the future

(Morgan Hardwood) #6

@Chintan_Gohel here’s a solution using Bash (available in Linux, macOS, and in Windows using MSYS2). It starts with the image’s full size and then subtracts a certain number of pixels from the crop width and height until the final crop size is about 1920x1080. How much gets subtracted at each step depends on the number of images - it’s calculated automatically. For every photo it creates a PP3 file with the correct crop parameters, e.g. photo123.raw will get a photo123.raw.pp3 sidecar file.

  1. Open a raw photo in RawTherapee, enable the crop tool but leave it at full size, save a partial PP3 containing only the crop parameters to the folder with your raw files, call it crop.pp3
  2. Save the Bash script below as rt_cropzoom, but first edit it in a text editor. All you need to do is to change *.raw to your raw file extension, e.g. if you use Nikon then change it to *.nef, and then change w=3888 and h=2608 to whatever size RT reports in crop.pp3 for W= and H=.
  3. Open a terminal, go into the folder containing your raw files and this script, make the script executable: chmod +x rt_cropzoom and then run it: ./rt_cropzoom

The script does not handle fractions, so the final size might not be exactly 1920x1080, it could be e.g. 1926x1087

The script:

#!/usr/bin/env bash
# This script iterates over a bunch of files, setting crop parameters for each
# until a final crop size of around 1920x1080 is reached.

# Edit these:

# Do not edit below this line ---
wStep=$((w - wFinal))
wStep=$((wStep / ${#files[@]}))
hStep=$((h - hFinal))
hStep=$((hStep / ${#files[@]}))

for f in "${files[@]}"; do
    wLess=$((wLess - wStep))
    hLess=$((hLess - hStep))
    printf '%s\n' "${f}: W=${wLess} H=${hLess}"
    sed -e "s/W=$w/W=$wLess/" -e "s/H=$h/H=$hLess/" crop.pp3 > "${f}.pp3"

printf '%s\n' "All done."

It requires a file crop.pp3 to be present in the same folder, example of such a file:

Orientation=As Image

(Glenn Butcher) #7

@Chintan_Gohel, given you want to do a lot of this, I recommend two things:

  1. Get one of the command line image programs, like gmic or Imagemagick
  2. Learn enough scripting in the operating system to run the command line program over your images.

The command line programs offer a more straightforward way to apply edits. Scripting can be daunting, but there’s a particular pattern that is useful, goes like this:

[collect list of images]
foreach image in list:
        [do the command line]

@Morgan_Hardwood gives the essence of it in bash, here’s the equivalent in Windows cmd shell using gmic and -crop:

echo off
setlocal enabledelayedexpansion

set x0=2
set y0=2
set x1=200
set y1=200
for %%F in (*.jpg) do (
    gmic %%F -crop !x0!,!y0,!,!x1!,!y1! -output foo/%%F
    set /A x0=x0+2
    set /A y0=y1+2
    set /A x1=x1-2
    set /A y1=y1-2

Gah, had to look up about a dozen things to get the above to work, windows cmd shell is a pain. For instance, !x0! says, 'evaluate x0 when you use it, not when the program starts - Really??? Oh, I used variable names consistent with the gmic command reference, and foo/ is a subdirectory under the current directory where the output images go.

A little complicated, but, it does your thing. Save the script; you can put all sorts of gmic (or imagemagick, etc.) things in it…


@Chintan_Gohel Welcome to the forum :slight_smile:!

I am thinking of a different approach to your query. If your goal is to make a time lapse video, it might be simpler to post-process in RT then do the cropping, positioning and/or zooming in the video editor (or an image editor/processor with video capabilities). Or do everything in the latter if you generate a lookup table. If you are unfamiliar with FOSS video editors, I am sure someone in the forum can give you some recommendations.

(Pat David) #9

This was going to be my suggestion as well - or at least to consider something along the lines of what @ggbutcher was suggesting with G’MIC or Imagemagick.

That is - bulk process the images for style, color, etc. Final output at full-sized resolution and further post processing for video output.

I think the biggest reason is that you may not know 100% what you’d like to do with the images in post after processing in RT. You may decide you want to crop but keep the lower left corner the same, as opposed to a push-zoom directly into the center. Processing full-sized images in RT and exporting now allows you to avoid that step the next time around during video editing…

Also, holy bash script, @Morgan_Hardwood! :wink:

(Morgan Hardwood) #10

I too think it is best to process the photos in RT and save them uncropped at full resolution, then do the zooming-in in Kdenlive, ShotCut, OpenShot, Blender, Flowblade, Natron or some other non-linear video editor.

I wrote the script because you gave me an excuse to write a script, and to a hammer every problem is a nail.

(Chintan Gohel) #11

to all who have replied, thank you - I’ll try using the code as soon as I get a new timelapse sequence where I want to use the code

I have a new issue though: I wanted to know how to combine multiple pairs of images into single images. For example image 1 and 2 into 1 complete image, then with images 3 and 4, 5+6, 7+8 etc

I searched online and got this site:

But I have no idea how to make the bash script work. In fact my bash console looks like this:

unlike yours in the photos above

Can anyone help me out?

Thank you

(Morgan Hardwood) #12

@Chintan_Gohel from this StackOverflow reply, just paste the part from “for” up to “done” into a console. It of course requires that your images are named “1.jpg” … “200.jpg”.

Now you have good reason to learn to script. This is a good guide:

(Mica) #13

I’d also assume that he’d need to install the necessary packages. apt install imagemagick

(Chintan Gohel) #14

I started getting lines and lines of this: The program ‘montage’ can be found in the following packages:

  • imagemagick
  • graphicsmagick-imagemagick-compat
    Try: sudo apt install

So how should I do that? I’m completely green in this area so forgive my ignorance

(Pat David) #15

From your prompt, you can update available packages by typing:

sudo apt-get update

Then, you should be able to install imagemagick as suggested:

sudo apt-get install imagemagick

This is all conjecture, as I see it’s Ubuntu/bash on windows (though I did just test this on my install and it appears to work).

After that, you can check if it installed by typing:


That should get you information about the command…

(Mica) #16

sudo apt install imagemagick

Edit: beaten by @patdavid :stuck_out_tongue:

(Chintan Gohel) #17

got it to install

Now a new error which seems obvious to me but I don’t know how to solve it: it can’t find the images which of course it can’t since it doesn’t know where to look for them. So how can I give it the location?

(Morgan Hardwood) #18


(Chintan Gohel) #19

I’ve tried working with that for the last 30 minutes - got nowehere - I’ll look into that further in the morning, it’s 10pm right now and I have to wake up at 5 for work

thank you for your time - hope I have something working tomorrow

(Chintan Gohel) #20

finally got the code to work and it’s useless - it just puts the 2 photos together with a lot of white space in between and thr file size is 7kB

Trying the other code with montage

How to combine pairs of images to single images?