PSCS JS ResizeImage

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

xbytor

PSCS JS ResizeImage

Post by xbytor »

Here is my library code for resizing images. Lots of different ways using:
1) the stock image resize call
2) Fit Image
3) the IMSS stairstep plugin

and a couple of other variants on this theme...

Code: Select all//--------------------------- ResizeImage ------------------------------------

ResizeImage = function ResizeImage() {}

ResizeImage.imssResize = function(doc, width, height, resolution,
                                  fit, ssSize, ssCount) {
  var ad;

  try { ad = app.activeDocument; } catch (e) {}
  app.activeDocument = doc;

  try {
    var id7 = stringIDToTypeID( "IM Software Group Stairstep Image Size" );
    var desc3 = new ActionDescriptor();

    if (!width && !height) {
      throw "width and/or height must be specified for resize";
    }

    if (width) {
      var id8 = charIDToTypeID( "Wdth" );
      var id9 = charIDToTypeID( "#Pxl" );
      desc3.putUnitDouble( id8, id9, width );
    }
   
    if (height) {
      var id10 = charIDToTypeID( "Hght" );
      var id11 = charIDToTypeID( "#Pxl" );
      desc3.putUnitDouble( id10, id11, height );
    }
   
    if (!resolution) {
      resolution = doc.resolution;
    }
    var id12 = charIDToTypeID( "Rslt" );
    var id13 = charIDToTypeID( "#Rsl" );
    desc3.putUnitDouble( id12, id13, resolution );   // required
   
    if (ssSize) {
      var id14 = charIDToTypeID( "siS%" );
      desc3.putInteger( id14,  ssSize);
    } else if (ssCount) {
      var id14 = charIDToTypeID( "siS#" );
      desc3.putInteger( id14,  ssCount);
    } else {
      var id14 = charIDToTypeID( "siS%" );   // default to 10%
      desc3.putInteger( id14,  10);
    }
   
    if (fit) {
      var id15 = charIDToTypeID( "siFI" );
      desc3.putBoolean( id15, true );
    } else {
      var id15 = charIDToTypeID( "siCP" );
      desc3.putBoolean( id15, true );
    }
    executeAction( id7, desc3, DialogModes.NO );

  } finally {
    if (ad) try { app.activeDocument = ad; } catch (e) {}
  }
}

//
// ResizeImage.pscsFit([doc,] width [, height])
// Examples:
// ResizeImage.pscsFit(500);      Fits the active doc to a 500x500 box
// ResizeImage.pscsFit(500, 600); Fits the active doc to a 500x600 box
// ResizeImage.pscsFit(doc, 500); Fits the doc to a 500x500 box
// ResizeImage.pscsFit(doc, 500, 600); Fits the doc to a 500x600 box
//
ResizeImage.pscsFit = function(arg1, arg2, arg3) {
  var doc, width, height;
  var ad;
  if (typeof arg1 == "object" && arg1 instanceof Document) {
    doc = arg1;
    width = arg2;
    height = arg3;
    try { ad = app.activeDocument; } catch (e) {}
    app.activeDocument = doc;
  } else {
    doc = app.activeDocument; // this could blow chunks
    width = arg1;
    height = arg2;
  }
  if (width == undefined) {
    return false;
  }
  if (height == undefined) {
    height = width;
  }
  try {
    var id7 = stringIDToTypeID( "3caa3434-cb67-11d1-bc43-0060b0a13dc4" );
    var desc3 = new ActionDescriptor();
    var id8 = charIDToTypeID( "Wdth" );
    var id9 = charIDToTypeID( "#Pxl" );
    desc3.putUnitDouble( id8, id9, width );
    var id10 = charIDToTypeID( "Hght" );
    var id11 = charIDToTypeID( "#Pxl" );
    desc3.putUnitDouble( id10, id11, height );
    executeAction( id7, desc3, DialogModes.NO );
  } finally {
    if (ad) try { app.activeDocument = ad; } catch (e) {}
  }
}

/
// Thanks to Nate Berggren for the original idea
//
// doc is required
// maxWidth or maxHeight is required
// resolution and method are optional
//
ResizeImage.resize = function(doc, maxWidth, maxHeight, resolution, method) {
  var width = doc.width.value;
  var height = doc.height.value;

  if (!maxWidth && !maxHeight) {
    return;
  }
  if (!maxWidth)  maxWidth = maxHeight;
  if (!maxHeight) maxHeight = maxWidth;

  if ((width/maxWidth) > (height/maxHeight)) {
    if (method == undefined) {
      method = (maxWidth > width) ?
         ResampleMethod.BICUBICSMOOTHER : ResampleMethod.BICUBICSHARPER;
    }
    doc.resizeImage(maxWidth, null, resolution, method);
  } else {
    if (method == undefined) {
      method = (maxHeight > height) ?
        ResampleMethod.BICUBICSMOOTHER : ResampleMethod.BICUBICSHARPER;
    }
    doc.resizeImage(null, maxHeight, resolution, method);
  }
}
// doc, maxWidth, and maxHeight are required
// resolution and method are optional
ResizeImage.invariantResize = function(doc, maxWidth, maxHeight,
                                       resolution, method) {
  var width = doc.width.value;
  var height = doc.height.value;
  var swap = false;

  if (width >= height && maxHeight > maxWidth) {
    swap = true;
  }
  if (width < height && maxHeight < maxWidth) {
    swap = true;
  }
  if (swap) {
    var tmp = maxHeight;
    maxHeight = maxWidth;
    maxWidth = tmp;
  }
  ResizeImage.resize(doc, maxWidth, maxHeight, resolution, method);
}

ResizeImage.psResize = function(doc, width, height, resolution, method) {
  doc.resizeImage(width, height, resolution, method);
}

ResizeImage.fileResize = function(doc, desiredSize) {
  var currentSize = doc.fullName.length;
  var ratio =  Math.sqrt(desiredSize / currentSize);
  var newW = Math.floor(doc.width.value * ratio);
  var newH = Math.floor(doc.height.value * ratio);

  ResizeImage.resize(doc, newW, newH)
    //  var method = (ratio > 1) ? ResampleMethod.BICUBICSMOOTHER : ResampleMethod.BICUBICSHARPER;

    //  doc.resizeImage(newW, newH, undefined, method);
}
wade

PSCS JS ResizeImage

Post by wade »

I wrote a script for myself to do stairstepping in PS7, have not got around to updating it for CS2 yet, but I think a scripted approach (looping through 10% increases {or decreases} until desired size is reached) gives better results than any plugin I have seen.

I did optimize it a bit for my purposes since I was only increasing the pixel count to get an image to print I would create a flattened duplicate of the source and then increase to the max bit depth (at the time 16). Of course attempting to maintain layers would make this more complicated.

The bit depth makes a huge difference in the end result. Even if you drop back to 8bit in the end the interim scaling operations have a superior look.


-wade
Norbert

PSCS JS ResizeImage

Post by Norbert »

Chris Cox (an Adobe programmer) has often stated that stair rezizing is no longer needed since PS CS. Bicubic Smoother or Sharper gives better results.
xbytor

PSCS JS ResizeImage

Post by xbytor »

If I have a large number of images to process/resize from the same source, I do take the time to check and see exactly which interpolation mechanism works best. In general I have found that if I am more than doubling the number of pixels in the image, stairstepping (any kind) works best. If it's double or less, I use the bicubic-smoother.

Fred Miranda's site has some good info on interpolation. There are other sites out there as well that do good comparative evaluations of different tools and methods.

This is one area where empircal evidence is king because of the very active research going on.
Mike Hale

PSCS JS ResizeImage

Post by Mike Hale »

xbytor wrote:This is one area where empircal evidence is king because of the very active research going on.

I agree with X.

Mr. Cox, as Norbert points out, works for Adobe. He could be biased. And rude.

opps, did I say that out loud
ngb

PSCS JS ResizeImage

Post by ngb »

xbytor, thanks for the credit in your code!



Nate