17 Replies Latest reply: Dec 18, 2012 7:21 AM by gokdenizkaradag RSS

    Batch HDR creation script


      I have created a Photoshop Script (using JavaScript) for batch creation of HDR images.

      I have used these previous discussions during implementation.


      In the beginning part of the script there are some options that you can change to customize the script's operation.

      You are free to modify this script and publish your modifications.


      The script can be found at the following URL (which is a git repository in itself) , and is also pasted below.




      // Batch Creation of HDR files
      // -----------------------------------------------------------------------------------
      // Opens a dialog box, asking for the container folder.
      // The container folder must have separate sub folders for each of the HDR image series
      // The output can be saved in a subfolder with an arbitrary name by
      // changing the options below.
      // example: to save hdr files as 
      // C:\container\hdrseries1\myhdrfolder\myhdr.psd
      // change the options HDROutputFolder to "myhdrfolder" 
      // and option HDRFileName to "myhdr"
      // Extensions of source image files
      var pictureExtensions = "*.jpg";
      // Sub-folder to save the resulting HDR file. This folder will be created if it does not exist.
      //var HDROutputFolder = "";  // disable subfolder like this
      var HDROutputFolder = "HDR"
      // Filename for saved HDR file. Do not set the extension here, extension is set automatically according to saveResultAsEXR option.
      var HDRFileName = "HDR";
      // Save EXR file (requires EXR plugin) If set to "false" saves the HDR as a  .PSD file
      var saveResultAsEXR = false;
      // Overwrite existing HDR output files? If set to "false" skips creating HDR files for already created image sets.
      // This is useful for adding some new HDR sets into the container folder, and running the script again 
      // to crate HDR files only for the newly added series.
      var overwriteExistingHDR = false;
      // --------------- End of settings ------------------- //
      var runMergeToHDRFromScript = true;// define and set to true before including Merge To HDR.jsx
      var g_ScriptFolderPath = app.path + "/"+ localize("$$$/ScriptingSupport/InstalledScripts=Presets/Scripts");
      var g_ScriptPath = File( g_ScriptFolderPath+'/Merge To HDR.jsx' );
      $.evalFile( g_ScriptPath );
      // function to manually save an EXR file. Requires EXR plugn to be installed
      function saveEXR(saveFile){
          var idsave = charIDToTypeID( "save" );
          var desc6 = new ActionDescriptor();
          var idAs = charIDToTypeID( "As  " );
          desc6.putString( idAs, "OpenEXR" );
          var idIn = charIDToTypeID( "In  " );
          desc6.putPath( idIn, saveFile );
          executeAction( idsave, desc6, DialogModes.NO );
      function main()
          var bracket_folders = [];
          var selected_folder = Folder.selectDialog ("Select the container folder. The container folder should have a separate folder inside for each HDR image series.")
          if (selected_folder != null)
              // to process multiple container folders, uncomment following line and edit folder paths.
              //bracket_folders = [ Folder('/c/images/bracket5'), Folder('/c/images/bracket9')];   
              bracket_folders = bracket_folders.concat(selected_folder);
              for (var f = 0; f < bracket_folders.length; f++)
                  var image_folders = bracket_folders[f].getFiles();
                  for (var g = 0; g < image_folders.length; g++)
                      var folder = image_folders[g];
                      var filelist = image_folders[g].getFiles(pictureExtensions);
                      var photoshopHDRFolder = Folder(folder.absoluteURI + "/" + HDROutputFolder);
                      if (saveResultAsEXR) {
                          var extension = ".exr";
                      } else {
                          var extension = ".psd";
                      var photoshopHDRFile = File(photoshopHDRFolder.absoluteURI + "/" + HDRFileName + extension);
                      if (overwriteExistingHDR ==false && photoshopHDRFile.exists == true) {
                      mergeToHDR.useAlignment = true;
                      mergeToHDR.outputBitDepth= 32;
                      mergeToHDR.mergeFilesToHDR( filelist, true, -2 );
                      if (photoshopHDRFolder.exists == false) {
                      if (saveResultAsEXR) {
                      } else {
                          activeDocument.saveAs(photoshopHDRFile );  // Use for PSD files
        • 1. Re: Batch HDR creation script

          Hi, this script looks great but it fails when I try and run it under both CS4 and CS5.1


          Cannot execute script in target engine 'main'!

          (#24) image_folders[g].getFiles is not a function



          I am running this under OSX.


          Any ideas?




          • 2. Re: Batch HDR creation script
            Paul Riggott Community Member

            If you are running this script from ExtendScript Toolkit make sure you select Photoshop from the top left hand dropdownlist.

            • 3. Re: Batch HDR creation script
              gokdenizkaradag Community Member

              You have to organize your files inside folders like this.




              `-> hdr1

              |      `-> 0.jpg , 1.jpg 2.jpg ....

              `-> hdr2

                     `-> 0.jpg , 1.jpg 2.jpg ....



              And your main_container folder must only include folders. It should not  contain single files. That may be your problem.


              You can debug it by putting alert(image_folders[g])

              just below the problematic line;

              var filelist = image_folders[g].getFiles(pictureExtensions);

              Probably you hava a file in main_container, and the alert will give you its name.

              • 4. Re: Batch HDR creation script
                Michael L Hale Community Member

                I agree the reason for the error is most likely bracket_folders[f] is not a Folder. I think a better fix would be to change


                var image_folders = bracket_folders[f].getFiles();




                if( image_folders[g] instanceof Folder) continue;// only process folders

                var image_folders = bracket_folders[f].getFiles();

                • 5. Re: Batch HDR creation script
                  gokdenizkaradag Community Member

                  Good idea,


                  I cannot edit my first post, but I have updated the gist:



                  download the file directly from here.

                  • 6. Re: Batch HDR creation script
                    karmadiI10 Community Member

                    Nice work. Very nice.

                    I have just completed a script that identifies bracketed sets from a within a folder.

                    The script you have written is a great next step to automate the HDR production process.


                    I have taken the time to calibrate the response curve of my camera sensor.

                    Is there a setting in the mergeToHDR API that lets a predefined response curve to be specified for the HDR creation process?

                    I'm using CS5.

                    • 7. Re: Batch HDR creation script
                      karmadiI10 Community Member

                      Have looked at the code of the MergeToHDR Pro script.

                      As far as I can tell the scripted process without a dialog allows Ghosting, Alignment, and source images to be specified only.

                      This means it is necessary to use the interactive dialog to choose a response curve preset.

                      • 8. Re: Batch HDR creation script
                        Michael L Hale Community Member

                        From looking at the script it seems to me that you might be able to skip the dialog and still use your responce curve preset. The script has a line..


                        const kMergeToHDRUIResponseCurve = app.charIDToTypeID( 'EmCV' );


                        You just need to know what charID or stringID to use to replace the defaut. You should be able to find that ID by running the script with the dialog and pulling apart the descriptor.

                        • 9. Re: Batch HDR creation script

                          Thank you for posting this script! I got the script to work, but I was wondering if there was script to write to make the HDR file have the Surrelistic effect.


                          I am new to javascript and any help would be much appreciated!




                          • 10. Re: Batch HDR creation script
                            gokdenizkaradag Community Member

                            As far as I could see, the scripting interface for HDR did not include tone mapping operators and effects.

                            You would have to simulate clicking on dialog box buttons to work with effects, like the saveEXR function.

                            • 11. Re: Batch HDR creation script
                              karmadiI10 Community Member

                              I spent a few hours reverse engineering the HDR Pro script in Photoshop.

                              The bad news is that the response curve can't be specified.

                              The code I refer to just loads a list of the available response curves (the ones previously saved in Photoshop's defulat directory) and passes it to the HDR merging module.

                              • 12. Re: Batch HDR creation script
                                karmadiI10 Community Member

                                Now that I have customised an HDR stacker script to handle long exposures, I am wondering why the "Process Collections" command doesn't work on the results.

                                My script creates groups of images that correspond to an HDR set, just like Adobe's script.

                                However "Process Collections" doesn't seem to recognise the groupings. Is there some metadata values that is set by the adobe stacker script?

                                • 13. Re: Batch HDR creation script



                                  I've been using your script quite successfully for some time (Thanks for it btw !), but it crashes from time to time. Mainly, it gives me an error that the images can not be aligned properly, or even worse, it's not saving the image and the script errors.

                                  Apparently, all the errors are coming from the Merge To HDR.jsx script, but if I'm launching the HDR merge by hand from bridge on the erroring images, it's working fine ...

                                  So I'm wondering if it would be some parameters that would need to be re-initialized or something else in order to get it work properly in the batch.


                                  Let me know if you have any idea why it would error like that.


                                  Thanks a lot,



                                  • 14. Re: Batch HDR creation script
                                    gokdenizkaradag Community Member

                                    Hi Julien,


                                    I'm not sure about the cause of the errors. I would recommend inspecting the "Merge To HDR.jsx" script itself to find out any parameters about alignment process, it has a very limited documentation and internet searches are not much helpful.


                                    Have you double checked the directory structure and made sure that similar image sequences are not intermixed ?

                                    • 15. Re: Batch HDR creation script

                                      Hi everyone,


                                      i try to run this script, also in the scripteditor with photoshop cs5,1 64 bit toolkit, but it doesnt work.

                                      after the first hdr merge it crashs and says that it can not achieve the function "main".

                                      it just create a empty folder in der first one.


                                      any idea? how can i solve the problem.


                                      thanks a lot,


                                      • 16. Re: Batch HDR creation script
                                        kim aldis

                                        I picked this script up this morning, using it to merge exposures for panoramas. Couple of things I found; first was the check for directories mentioned earlier, I dropped in a check for that. The second was that the merged images were coming out looking horribly flat in the skies. It took me a while to work out that it was the third argument to the mergeToHDR.mergeFilesToHDR() method, ghosting. For now I've changed it to null, which is obviously not great but I thought it might be a useful pointer to someone having the same problem.


                                        Finally, rather than 30 folders of 3 EV brackets each, I rejigged the code so that it looks in a single folder where the images are ordered EV1, EV2, EV3, EV1, EV2, EV3 and works it all out. It makes life quicker for me. If anyone's interested I'll post it here once I've tidied my horribly ugly code.


                                        Big thanks to the contributors to this.

                                        • 17. Re: Batch HDR creation script
                                          gokdenizkaradag Community Member

                                          Hi kim aldis,


                                          if you can make your changes compatible with the previous code. (it works either if the files are in directories, or ordered by name) that would be great. Thanks for your further work on the script.