Question about automating cropping process

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

@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
    Done.

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:
files=(*.raw)
w=3888
h=2608

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

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"
done

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

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

[Crop]
Enabled=true
W=3888
H=2608
FixedRatio=true
Ratio=3:2
Orientation=As Image
2 Likes

@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]
done

@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.

1 Like

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:

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.

1 Like

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: https://stackoverflow.com/questions/23686427/imagemagick-how-to-combine-200-images-in-pairs-of-2-side-by-side-book-layout/45016635#45016635

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

@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: BashGuide - Greg's Wiki

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

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

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:

montage

That should get you information about the command…

sudo apt install imagemagick

Edit: beaten by @patdavid :stuck_out_tongue:

1 Like

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?

@Chintan_Gohel cd (command) - Wikipedia

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

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

I trie this code: montage *.jpg -tile 2x -background white -geometry +2+2 result.jpg

It results in one big file that has all the images in it- and there’s a gap between pairs

If I try with one pair of photos only, the gap is still there between the 2 photos and the new file size is much smaller than the original file. Original files are 450kB each but resulting file is 198kB

Don’t know what to do now - and I have 800 pairs of photo to join

I’m going to post this afresh as it’s own question

Hey, so I finally got to try out your code for the progressive crop.

I’ve run into these errors and I was hoping you could help me out? If it isn’t too much trouble

Thank you

")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
./rt.txt: line 17: syntax error near unexpected token $'do\r'' '/rt.txt: line 17: for f in “${files[@]}”;do

@Chintan_Gohel

  1. Don’t quote whole posts, what’s the point.
  2. It looks like the text editor you used did something “smart” like replacing the programmer’s quotes "x" with curly quotes “x”
  3. It’s bad practice to store executable scripts with a .txt extension.

I’m using notepad and it saves as a txt file by default - however, I was able to remove the txt extension but I’m getting the same errors

So it comes down to using a different editor - I’m installing notepad++

Edit - the same errors happened with notepad++