5 Replies Latest reply: Apr 11, 2012 1:15 AM by pooolve RSS

    Script for making random thumbnails from single image

    pooolve Community Member

      Hi all,

       

      I need something like a hundred different thumbnails from each image in a series of images, that is, hundred random sections of the same image, saved in a folder as jpg,  and i was hoping that i could find a script for this.

       

      What the script has to do is:

       

      1. select the size of the crop (this would also be the dimensions of the thumbnail saved)
      2. rotate crop selection in a random orientation and place the crop randomly on the canvas
      3. save the image as a jpg in a folder
      4. return to original image,
      5. repeat process x times before quitting script.

       

      I dont think this should be to difficult to make a script for, but unfortunately i don´t know how to code.

       

      Is there anybody that could help me with this?

       

      This would save me a lot of time!

        • 1. Re: Script for making random thumbnails from single image
          c.pfaffenbichler Community Member

          I dont think this should be to difficult to make a script for

          Maybe because you have not considered the difficulty of calculating the offset of the rotated content so as not to exceed the existing image’s edge – or maybe your math-skills are just better than mine.

           

          In any case it should be possible to achieve the intended result.

          What do you need it for, if I may ask?

           

          Edit: Not 100% sure about the randomization, but the resulting files look fairly random …

          rotatedSlicesScr.jpg

          • 2. Re: Script for making random thumbnails from single image
            pooolve Community Member

            dear, C.Pfaffenbichler,

             

            This looks really good - exactly what i was looking for! Is it possible to chose the dimensions and number of thumbnails? If so, its perfect!

             

            It´s something i want to try out for an art project - the intention is to use the thumbnails as a base for a series of drawings, so its not a big problem if not all the thumbnails are inside the format. I just need a big selection.

             

            Could you send me the code? That would be amazing!

             

            Thanks,

            • 3. Re: Script for making random thumbnails from single image
              c.pfaffenbichler Community Member

              You can give this a try:

              // create copies with pseudo random clipped and rotated parts of image;
              // thanks to xbytor; 
              // 2012, use at your own risk; 
              #target photoshop
              if (app.documents.length > 0) {
              var originalRulerUnits = app.preferences.rulerUnits;
              app.preferences.rulerUnits = Units.POINTS;
              ////////////////////////////////////
              // set name for folder to save jpgs to;
              var folderName = "rotatedJpgs";
              // set number of jpgs;
              var theNumber = 100;
              // width and height;
              var theWidth = 500;
              var theHeight = 500;
              // calculate some values;
              var theDiagonal = Math.sqrt ((theWidth * theWidth) + (theHeight * theHeight));
              var diagAngle = angleFromRadians(Math.acos((theWidth) / (theDiagonal)));
              ////////////////////////////////////
              // get file name and path;
              var myDocument = app.activeDocument;
              var docName = myDocument.name;
              try {
                        var basename = docName.match(/(.*)\.[^\.]+$/)[1];
                        var docPath = myDocument.path;
                        }
              catch (e) {
                        basename = docName;
                        var docPath = "~/Desktop"; 
                        };
              // create folder if it does not exist yet;
              if (Folder(docPath + "/" + folderName).exists == true) {
                        var docPath = docPath + "/" + folderName;
                        }
              else {
                        var theFolder = Folder(docPath + "/" + folderName).create();
                        var docPath = docPath + "/" + folderName;
                        };
              // document dimensions;
              var docWidth = myDocument.width;
              var docHeight = myDocument.height;
              // jpg options;
              var jpegOptions = new JPEGSaveOptions();
              jpegOptions.quality = 10;
              jpegOptions.embedColorProfile = true;
              jpegOptions.matte = MatteType.NONE;
              // duplicate image;
              var theCopy = myDocument.duplicate (theCopy, true);
              var origResolution = theCopy.resolution;
              theCopy.resizeImage(undefined, undefined, 72, ResampleMethod.NONE);
              var docHalfWidth = theCopy.width / 2;
              var docHalfHeight = theCopy.height / 2;
              var theLayer = smartify2010(theCopy.layers[0]);
              theCopy.resizeCanvas (theWidth, theHeight, AnchorPosition.MIDDLECENTER);
              var theHistoryState = theCopy.activeHistoryState;
              // do the variations;
              for (var m = 0; m < theNumber; m++) {
              var theAngle = Math.random() * 360;
              theLayer.rotate (theAngle, AnchorPosition.MIDDLECENTER);
              //theCopy.resizeCanvas (theWidth, theHeight, AnchorPosition.MIDDLECENTER);
              // get tolerance offset;
              var theHor1 = Math.abs(Math.cos(radiansOf(theAngle + diagAngle)) * theDiagonal / 2);
              var theVer1 = Math.abs(Math.sin(radiansOf(theAngle + diagAngle)) * theDiagonal/ 2);
              var theHor2 = Math.abs(Math.cos(radiansOf(theAngle - diagAngle)) * theDiagonal / 2);
              var theVer2 = Math.abs(Math.sin(radiansOf(theAngle - diagAngle)) * theDiagonal / -2);
              // calculate max offset for unrotated overall rectangle;
              var thisHalfWidth = docHalfWidth - Math.max(theHor1, theHor2);
              var thisHalfHeight = docHalfHeight - Math.max(theVer1, theVer2);
              // calculate random offset for unrotated overall rectangle;
              var randomX = thisHalfWidth * (Math.random() - 0.5) * 2;
              var randomY = thisHalfHeight * (Math.random() - 0.5) * 2;
              var aDiag = Math.sqrt (randomX * randomX + randomY * randomY);
              var anAngle = angleFromRadians(Math.asin((randomY) / (aDiag))) + theAngle;
              anAngle = anAngle + Math.floor(Math.random() * 2) * 180;
              // calculate  offset for rotated overall rectangle;
              var offsetX = Math.cos(radiansOf(anAngle)) * aDiag;
              var offsetY = Math.sin(radiansOf(anAngle)) * aDiag;
              //alert (theAngle+"\n\n"+offsetX +"\n"+ offsetY+"\n\n"+ thisHalfWidth+"\n"+thisHalfHeight);
              theLayer.translate(offsetX, offsetY);
              theCopy.resizeImage(undefined, undefined, origResolution, ResampleMethod.NONE);
              theCopy.saveAs((new File(docPath+"/"+basename+"_"+bufferNumberWithZeros(m+1, 3)+".jpg")),jpegOptions,true);
              theCopy.activeHistoryState = theHistoryState;
              };
              // clean up;
              theCopy.close(SaveOptions.DONOTSAVECHANGES);
              app.preferences.rulerUnits = originalRulerUnits;
              };
              ////////////////////////////////////
              ////// radians //////
              function radiansOf (theAngle) {
                        return theAngle * Math.PI / 180
                        };
              ////// radians //////
              function angleFromRadians (theRad) {
                        return theRad / Math.PI * 180
                        };
              ////// buffer number with zeros //////
              function bufferNumberWithZeros (number, places) {
                        var theNumberString = String(number);
                        for (var o = 0; o < (places - String(number).length); o++) {
                                  theNumberString = String("0" + theNumberString)
                                  };
                        return theNumberString
                        };
              ////// function to smartify if not //////
              function smartify2010 (theLayer) {
              // make layers smart objects if they are not already;
                        app.activeDocument.activeLayer = theLayer;
              // process pixel-layers and groups;
                    if (theLayer.kind == "LayerKind.GRADIENTFILL" || theLayer.kind == "LayerKind.LAYER3D" || theLayer.kind == "LayerKind.NORMAL" || 
                    theLayer.kind == "LayerKind.PATTERNFILL" || theLayer.kind == "LayerKind.SOLIDFILL" || 
                    theLayer.kind == "LayerKind.TEXT" || theLayer.kind == "LayerKind.VIDEO" || theLayer.typename == "LayerSet") {
                                  var id557 = charIDToTypeID( "slct" );
                                  var desc108 = new ActionDescriptor();
                                  var id558 = charIDToTypeID( "null" );
                                  var ref77 = new ActionReference();
                                  var id559 = charIDToTypeID( "Mn  " );
                                  var id560 = charIDToTypeID( "MnIt" );
                                  var id561 = stringIDToTypeID( "newPlacedLayer" );
                                  ref77.putEnumerated( id559, id560, id561 );
                                  desc108.putReference( id558, ref77 );
                                  executeAction( id557, desc108, DialogModes.NO )
                                  return app.activeDocument.activeLayer
                                  }
                        if (theLayer.kind == LayerKind.SMARTOBJECT || theLayer.kind == "LayerKind.VIDEO") {return theLayer};
                        };
              ////// get an angle, 3:00 being 0˚, 6:00 90˚, etc. //////
              function getAngle (pointOne, pointTwo) {
              // calculate the triangle sides;
                        var width = pointTwo[0] - pointOne[0];
                        var height = pointTwo[1] - pointOne[1];
                        var sideC = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)); 
              // calculate the angles;
                        if (width+width > width) {theAngle = Math.asin(height / sideC) * 360 / 2 / Math.PI}
                        else {theAngle = 180 - (Math.asin(height / sideC) * 360 / 2 / Math.PI)};
                        if (theAngle < 0) {theAngle = (360 + theAngle)};
                        return theAngle
                        };
              
              • 4. Re: Script for making random thumbnails from single image
                c.pfaffenbichler Community Member

                By the way, you need to adapt

                var theNumber = 100;
                var theWidth = 500;
                var theHeight = 500;

                according to your needs.

                 

                The names should be clear enough, width and height are in pixels.

                • 5. Re: Script for making random thumbnails from single image
                  pooolve Community Member

                  Worked like a charm! Thanks alot!!