0

I am using the following script for drawing a set of paths onto a photoshop image.

    var docRef= app.activeDocument;
    app.displayDialogs = DialogModes.NO;

    var currentRulerUnits = app.preferences.rulerUnits;
    var currentTypeUnits = app.preferences.typeUnits;
    var currentDialogMode = app.preferences.displayDialogs;

    app.preferences.rulerUnits = Units.PIXELS;
    app.preferences.typeUnits = TypeUnits.PIXELS;


    var height= docRef.height;
    var width = docRef.width;


    var isHorizontal = false;
    var backgroundlayer = docRef.artLayers.getByName("Background");

    var layer1,layer2,layer3,layer4;
    if(isHorizontal)
    {
        // Copy and paste the layers in such a way that they are in a horizontal orientation.
        layer1 = backgroundlayer.duplicate();
        layer1.resize(120,120,AnchorPosition.TOPCENTER)
        layer1.name = "A"
        layer1.translate(0,height*(-0.5));

        layer2 = backgroundlayer.duplicate();
        layer2.resize(120,120,AnchorPosition.BOTTOMCENTER)
        layer2.name= "B"
        layer2.translate(0,height*(0.5));

        layer3 = backgroundlayer.duplicate();
        layer3.resize(120,120,AnchorPosition.TOPCENTER);
        layer3.name = "C";
        layer3.translate(0,height*(-0.5));

        layer4 = backgroundlayer.duplicate();
        layer4.resize(120,120,AnchorPosition.BOTTOMCENTER);
        layer4.name= "D";
        layer4.translate(0,height*(0.5));
    } 
    else 
    {
        layer1 = backgroundlayer.duplicate();
        layer1.resize(120,120,AnchorPosition.MIDDLELEFT);
        layer1.name = "A";
        layer1.translate(width*(-0.5),0);

        layer2 = backgroundlayer.duplicate();
        layer2.resize(120,120,AnchorPosition.MIDDLERIGHT);
        layer2.name= "B";
        layer2.translate(width*(0.5),0);

        layer3 = backgroundlayer.duplicate();
        layer3.resize(120,120,AnchorPosition.MIDDLELEFT);
        layer3.name = "C";
        layer3.translate(width*(-0.5),0);

        layer4 = backgroundlayer.duplicate();
        layer4.resize(120,120,AnchorPosition.MIDDLERIGHT);
        layer4.name= "D";
        layer4.translate(width*(0.5),0);
    }

    var topXspread = .18; // 25 % Percentage expressed as decimal
    var topYhighspread = 0.2; // 20 %
    var topYlowspread = 0.6; // 80%
    var yminspread = 0.05; // 5 %
    var smalltoffset = yminspread/2;
    var overallYoffset = 100; // units are in pixels
    var midpointYoffset = -75;

    var point1 = GeneratePathPointInfo(Math.floor((width/2) * (1 - topXspread)),overallYoffset+Math.floor(height/2 *(1 + yminspread)));
    var point2 = GeneratePathPointInfo(Math.floor((width/2)),overallYoffset+Math.floor(height*(topYhighspread)));
    var point3 = GeneratePathPointInfo(Math.floor((width/2)*(1 + topXspread)),overallYoffset+Math.floor(height/2 *(1 + yminspread)));
    var point4 = GeneratePathPointInfo(Math.floor((width/2)),overallYoffset+Math.floor(height* topYlowspread));
    var point5 = GeneratePathPointInfo(Math.floor((width/2)),midpointYoffset+overallYoffset+Math.floor(height/2));


    GenerateTrianglePathItem(point1,point2,point4,"bigtleft",docRef);
    GenerateTrianglePathItem(point2,point3,point4,"bigtright",docRef);
    GenerateLinePathItem(point2,point4,"vertical",docRef);
    GenerateLinePathItem(point1,point5,"lineleft",docRef);
    GenerateLinePathItem(point3,point5,"lineright",docRef);


    var point1small = GeneratePathPointInfo(Math.floor(width/2 * (1 - topXspread)),overallYoffset+Math.floor((height/2 *(1 + yminspread))+ (height *smalltoffset)));
    var point3small = GeneratePathPointInfo(Math.floor(width/2*(1 + topXspread)),overallYoffset+Math.floor((height/2 *(1 + yminspread))+ (height *smalltoffset)));
    var point4small = GeneratePathPointInfo(Math.floor(width/2),overallYoffset+Math.floor((height* topYlowspread)+ (height *smalltoffset)));
    var point6small = GeneratePathPointInfo(Math.floor(width/2),overallYoffset+Math.floor((height* topYlowspread)+ (height * 6 * smalltoffset)));
    GenerateTrianglePathItem(point1small,point4small,point6small,"smalltleft",docRef);
    GenerateTrianglePathItem(point3small,point4small,point6small,"smalltright",docRef);
    GenerateLinePathItem(point4small,point6small,"verticalsmall",docRef);

    function GenerateTrianglePathItem(point1,point2,point3,name,docRef){
        var spi = new SubPathInfo();
        spi.closed = true;
        spi.operation = ShapeOperation.SHAPEXOR;
        spi.entireSubPath = [point1,point2,point3];
        var line = docRef.pathItems.add(name, [spi]);
        };

    function GenerateLinePathItem(point1,point2,name,docRef){
        var spi = new SubPathInfo();
        spi.closed = true;
        spi.operation = ShapeOperation.SHAPEXOR;
        spi.entireSubPath = [point1,point2];
        var line = docRef.pathItems.add(name, [spi]);
        };

    function GeneratePathPointInfo(x,y){
        var startPoint = new PathPointInfo();
        startPoint.anchor = [x,y];
        startPoint.leftDirection = [x,y];
        startPoint.rightDirection = [x,y];
        startPoint.kind = PointKind.CORNERPOINT;
        return startPoint;
        };

The co-ordinates for the points (Point1, 2, 3 ..) are being calculated correctly and are withing the bounds of the image. However, the final path drawn is being drawn completely outside the image bounds.

Can anyone help me with why this is happening so?

  • I am guessing it has something to do with different co-ordinates units used for images and paths. But I am not able to pin-point how to solve the problem. – Raghavendra Kumar Apr 25 '16 at 16:50
  • Ok. When I set the PPI of the image to 72, everything works. Can anyone explain why this happens and how I can modify my script to ensure that it works with images of Any PPI – Raghavendra Kumar Apr 25 '16 at 16:55
  • 1
    A quick guess.. your x/y coordinates are using points instead of pixels. at 72PPI 1px == 1pt so it would make sense. I'm more used to scripting in AI and havn't got time to test now but that's what I would look at. – Cai Apr 25 '16 at 17:58
  • Can you explain why 72 ppi implies 1 point is 1 pixel? – Raghavendra Kumar Apr 26 '16 at 3:44
  • 1
    You can easily see this by setting up a PSD at 72ppi, and switching the rulers from pixels to points, the numbers will be the same. Change the resolution to 300ppi (or anything else) without changing the pixel size. Switch the rulers from pixels to points again and the points will be different. – Cai Apr 26 '16 at 6:04
1

I'm not used to scripting with Photoshop so I'm not sure if there is a way to get PathPointInfo to work in pixels—seems there should be—but I can't find it.

One solution is to just change the document resolution to 72PPI. You can save the initial resolution at the beginning of the script, change to 72PPI then change back to the initial resolution once you are finished. I just tested this and it works with no problem.

var docRef= app.activeDocument;
app.displayDialogs = DialogModes.NO;

// Starting resolution
var startResolution = docRef.resolution;

// Change resolution to 72ppi without changing pixel size
docRef.resizeImage(undefined, undefined, 72, ResampleMethod.NONE );


// REST OF YOUR CODE...


// Reset back to starting resolution
docRef.resizeImage(undefined, undefined, startResolution, ResampleMethod.NONE );
  • I have actually implemented this temporarily. However making the image from, say 300 ppi to 72 ppi, will it not affect the quality of the image? Even if we change the ppi back to original value. – Raghavendra Kumar Apr 26 '16 at 6:55
  • 1
    No. as long as you don't change the pixel dimensions (ResampleMethod.NONE) there will be no affect at all. – Cai Apr 26 '16 at 6:56
  • Awesome. Can you point me to any resources on how image handling, ppi and other details work? – Raghavendra Kumar Apr 26 '16 at 6:57
  • 1
    This post has some good info on resolution. Generally speaking PPI doesn't matter unless you are printing, all that matters on screen is pixel dimensions. – Cai Apr 26 '16 at 7:01
  • 1
    No problem. Glad to help :) – Cai Apr 26 '16 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.