I made a small python script for dealing with CR3 files. (dnglab + hdrmerge)

Hello everyone !
It’s only recently that I discovered HDRMerge and I love :

  • the batch mode
  • normal size 16bit HDR files
  • the GUI to handle ghosts.

But I use Canon cameras with CR3 files. That is why I made myself a little python script (nothing fancy, I’m a total beginner in python) to handle things with some automation.

It works this way :

  • I put the CR3 files paths that I want to exploit in a txt file (easy for to grab them with doublecommander).
  • the txt file is red to grab the CR3 files and dnglab convert them files to DNG
  • DNG files are fed to hdrmerge batchmode. HdrMerge Parameters can easily be changed in the script.
  • at the end of the script, there is the possibility to delete the DNG files (not the HDR ones of course).
    dnglab_hdrmerge.py (2.8 KB)
5 Likes

Hi, @yopyop95, and welcome to the forum!

Great stuff! I’m not a Canon shooter myself, but I daresay there are many that will find your script very useful. Thanks for sharing! :+1:

Thanks for your kind words. :slight_smile:

I should add that this is a script for linux users. For windows, I guess that the direct paths to dnglab.exe and hdrmerge.exe should be used.

1 Like

Hello everyone,

I made a new version of my script to handle easily the creation of HDR files with HDRmerge from Canon CR3 files.
Now, at the beginning of the script you can chosse between two options :

  • F option (File) : reading from a txt file that contains the CR3 files full paths that you want to work with

  • T option (Time) :
    - a) the script detects the directories that are new or that have seen some activities (files modified or new ones created inside of it)
    - b) after choosing the directory you want to work with, the script will detect only the new CR3 files that are inside and feed them to DNGlab and then HDRMerge in batch mode.

Parameters that have to be set in the script :

  • for the FILE mode : link to the txt file that gives the CR3 file paths.
  • for the TIME mode : path to your whole photo directory and the time interval in minutes to filter in “new files”.

HDRMerge parameters can also be modified : gap, bits per sample and previewsize.

Again this is beginner python code, it’s not beautiffuly written but it works :slight_smile: What I love is the great simplicity of DNGLab and the magic of the HDRMerge batch mode. I am now only using these tools instead of the HDR tool in Darktable. The small file sizes are great.
dnglab_hdrmerge_v0.4.py (8.4 KB)

1 Like

I am wondering if these tools can handle alignment of hand held images or if a tripod must be used?

2 Likes

@Terry:

HDRMerge will correct small misalignments by translation. So, if your camera allows it, you can take the shots with bracketing in burst mode. I have successfully done this just holding the camera with my hands, but using a tripod is highly recommended to obtain the best results.
(GitHub - jcelaya/hdrmerge: HDR exposure merging)

1 Like

Yes, HDRMerge has the option to correct misalignments. IIRC corrections in translations can be done but it does not work in rotation. I included the option in the script : you can choose to activate auto-alignement or not.

I am showing total ignorance here. I want to test out this script. I presume I have to have both HDRMerge and dnglab installed. I believe I have done that but when I run dnglab I get the flash of a very quick screen and then nothing.

where do I place the script you wrote? I am on a windows computer.

Hello Terry,

I do not know windows very well regarding Python. I hope I can help, here’s what I think.

  • Do you you have python installed on your pc ? You need to in order to be able to execute the script. The script could be converted to an .exe but in this case you wouldn’t be able to modify the code, so I prefer to stay with *.py script files.

  • Once Python installed you should be able to execute the script in the command line interface :

python C:\path\to\dnglab_hdrmerge_0.4.py

  • Before that, you have to make some (really easy) tweaks in the code to adapt it to windows : replace ‘dnglab’ with ‘C:\path\to\dnglab.exe’ and ‘hdrmerge’ with ‘C:\path\to\hdrmerge.exe’

Anyone please correct me if I’m wrong.

I have installed Python and when I click on your script I get this


But when I type either Y or N and click on enter it just all disappears. I just don’t seem to know what I am doing. I hope someone can guide a dunderhead of a windows user in how to run this script on Windows 11.

I was unsure where to replace replace ‘dnglab’ with ‘C:\path\to\dnglab.exe’ and ‘hdrmerge’ with ‘C:\path\to\hdrmerge.exe’. This is what I tried, but it didn’t help.

Use double backslash in place of a single one.

More information needed. I tried
image

and I also tried the double slash after C: but neither worked.

In Python, and most languages, \ is often used in combination with letters to create special characters. For example, \n would mean a new-line character, \t would mean Tab.
So, in Python, if we have a variable called path, the following would set it:
path = 'C:\\path\\to\\hdrmerge.exe'
or
path = "C:\\path\\to\\hdrmerge.exe"

So, you either add the executables to your PATH variable, and then you don’t need to change anything, or you have to modify for example this line:

cmd:str = "hdrmerge -g " + gap + " -b "+ bps + " -p " + previewsize + " -a --batch " + every_files

This would probably work:

cmd:str = "C:\\path\\to\\hdrmerge.exe -g " + gap + " -b "+ bps + " -p " + previewsize + " -a --batch " + every_files

Setting PATH may be more convenient: any program whose directory is on the search path can be invoked without specifying its full path. That’s how it’s also done on Linux.

BTW, as far as I know, Windows can also handle / as the path separator. For example:
https://answers.microsoft.com/en-us/windows/forum/all/when-did-windows-start-accepting-forward-slash-as/aa033741-0fc6-43e3-ab6e-165020794126

Hmm… Is the problem metadata? HDRMerge uses libraw so it should support CR3s if built against recent libraw.

Terry, each line that starts with ‘#’ is ignored, these are comments.

These are the lines that need to be changed :
line 157

           "dnglab",

line 174

cmd:str = "hdrmerge -g " + gap + " -b "+ bps + " -p " + previewsize + " -a --batch " + every_files

line 178

cmd:str = "hdrmerge -g " + gap + " -b "+ bps + " -p " + previewsize + " -a --batch --no-align --no-crop " + every_files

Also, Kofa is right, that’s why instead of ‘\’ or even ‘\\’ it’s best to use ‘/’ in this case

C:/path/to/hdrmerge.exe

So you have something like that :

 cmd:str = "C:/path/to/hdrmerge.exe -g " + gap + " -b "+ bps + " -p " + previewsize + " -a --batch " + every_files

On ArchLinux, I installed hdrmerge from AUR https://aur.archlinux.org/packages/hdrmerge and CR3 files are not handled ; you get this error :

Exiv2 error: /path/to/IMG_xxxx.CR3: The file contains data of an unknown image type

And of course, Darktable is on my machine, handling CR3 files just fine. Exiv2 is up to date (0.28.2-4).

¯\(ツ)

1 Like

I am making some progress. Sorry for the struggle on my part here. I have downloaded your v0.4.py script and modified the paths to dnglab and hdrmerge. Now when I start the script I get further into it but am struggling with picking the directory to look for new CR3 files. I have used the darktable copy feature to copy three CR3 files to a folder called HDR merge which is located in the pictures folder on my X drive. I presume I have typed this wrong. Your patience and help would be appreciated.

Use single forward slash in the path, not double. So X:/some/path, not X://some//path. The doubling is only needed for backslash, to indicate you are not using it to create special characters (newline, tab etc.). Forward slash has no such special behaviour, and even for backslash, the special behaviour applies to source code, not keyboard input.

You can read more here, if you want, but it’s not needed, strictly speaking:

I’d also avoid spaces in the path, they can be problematic. I don’t know if that is the case for this script, but better safe than sorry. I usually use underscores instead.

No problem Terry, happy to help. If you want to use the “time” function, you can just write on line 29 :

source_file:str = “X:/Pictures (X)/”

This folder will be scanned and then you’ll have the option to choose ‘X:/Pictures (X)/HDR merging’ in the “LISTE” (sorry, it’s not fully translated to english) because you recently made some modifications to it.

You could also use the File function and have something like this. On line 26 :

source_file:str = “X:/Pictures (X)/cr3_dng_hdr.txt”

And in this file you fill the name of your files, which could give something like this:

X:/Pictures (X)/HDR merging/IMG_9546.CR3
X:/Pictures (X)/HDR merging/IMG_9547.CR3
X:/Pictures (X)/HDR merging/IMG_9549.CR3
X:/Pictures (X)/HDR merging/other stuff/IMG_9549.CR3
X:/Pictures (X)/HDR merging/other stuff/IMG_9550.CR3
X:/Pictures (X)/HDR merging/other stuff/IMG_9551.CR3

From my point of view, “File mode” is useful when you want to deal with old files from previous photo sessions, whereas the “time mode” is easier when you just transfered your new photos on your harddrive. The great stuff in all of this is the magic of the hdrmerge batch mode, with its detection of group of photos.

PS : like Kofa said, it is advised to avoid whitespaces in the directory structures. My code is some beginner level stuff, I didn’t make it proof to every possible situation, only the usual.

I will get back to trying your script soon. Thanks for the reply. I lost internet yesterday so I didn’t receive anyone’s replies. Instead I tried using Hugin for the first time for HDR merging. It did a very reasonable job with hand held images. I see it also does focus stacking. I want to explore Hugin and HDR merge to find the best HDR merge program for the rare occasion I need it. The Canon software did a disappointing job. I do a lot of hand held bracketed shots and occasionally HDR merge can help. But with modern sensors this has become less of an issue as the cameras capture a wider dynamic range compared to my 2005 cameras.

1 Like