[FIXED] Bilder, die bei Verwendung für die Schleife nicht richtig ausgerichtet sind

Ausgabe

Ich erstelle eine Stickerseite mit verschiedenen Größen eines Bildes. Der Aufkleber muss sich innerhalb des orangefarbenen Kreises mit einem Radius von 1000 Pixel befinden.

Um sicherzustellen, dass die Ausrichtungen korrekt sind, habe ich einen schwarzen Testrand hinzugefügt, um visuell zu sehen, wie er angeordnet wird.

So war es angelegt. Es ist wunderschön.

Also dachte ich, das Drehbuch sei fertig. Ich habe es zum Laufen gebracht, aber jetzt ohne die schwarzen Ränder. So kam es …

Sehen Sie, wie es in der unteren rechten Ecke beschnitten ist? Ich habe das Gefühl, dass es etwas mit der “Mathe” meiner For-Loop-Codes zu tun hat, die ich nicht herausfinden kann … Hier sind meine Codes:

    // Page 3: Third document. =====================================================
    // Note: Need to re-initialize as the variables are referencing to the previous document.
    //       Error shown: The requested action requires that the target document is the frontmost document.
    var doc = app.activeDocument;
    var layer = doc.activeLayer;
    // Get the variables to work with.
    var width       = doc.width.as('px');
    var height      = doc.height.as('px');
    var bounds      = app.activeDocument.activeLayer.bounds;
    var layerWidth  = bounds[2].as('px') - bounds[0].as('px');
    var layerHeight = bounds[3].as('px') - bounds[1].as('px');
    var resolution  = doc.resolution;

    // Create bleed guides.
    doc.guides.add(Direction.HORIZONTAL, bleed);
    doc.guides.add(Direction.VERTICAL, bleed);
    doc.guides.add(Direction.HORIZONTAL, height - bleed);
    doc.guides.add(Direction.VERTICAL, width - bleed);

    // Third auto-page workings.
    // x1 biggest.
    doc.paste();  // Paste the selection.
    doc.activeLayer.translate(0 - width / 4, 0 - height / 4);  // Move layer to the top left corner. First copy.
    var big_copy = doc.activeLayer;  // Copy the selection.
    // x4 mid sized.
    for (var i = 0; i < 2; i++) {
        for (var j = 0; j < 2; j++) {
            copy = doc.activeLayer.duplicate();
            copy.translate(0 + width / 4 * i, (0 + height / 2) + (height / 4 * j));
            copy.resize(50, 50, AnchorPosition.TOPLEFT);
        }
    }
    // Many minis.
    for (var i = 0; i < 3; i++) {
        for (var j = 0; j < 6; j++) {
            copy = big_copy.duplicate();
            copy.translate((0 + width / 2) + (width / 2 / 3 * i), 0 + (height / 6 * j));
            copy.resize(100 / 3, 100 / 3, AnchorPosition.TOPLEFT);
        }
    }

    doc.artLayers.getByName('Background').remove();  // Delete the Background layer.
    mergeVisible();  // Merge visible.
    freeTransform(0, 0, (width - bleed * 2) / width * 100, (height - bleed * 2) / height * 100);  // Resize selection within bleeds.

Edit: [More info] The black border is merged with the dino image before the automation. So, I’m guessing why it’s working correctly with the border was because the merged image had a 1000px by 1000px in dimension. The moment I removed the black borders, it broke. It could be due to the anchor position… But I’m not sure.

Solution

I modified your code a little: partly the issue was that the script was merging all the visible layers and when some of the layers were outside document bounds they weren’t the part of this merge. So

  • on line 67 I create a group to contain all the layers
  • on lines 75, 86 and 97 all the duplicates are added to that group
  • on line 105 instead of mergeVisible() I select the group and merge it down with mergeDown()
  • and then a tad later for free transform I use bounds of this new layer

Here’s the result:

Geben Sie hier die Bildbeschreibung ein

and the code:

var maintainAspectRatio; // Set to true to keep aspect ratio.
maintainAspectRatio = true;
if (app.documents.length > 0)
{
    app.activeDocument.suspendHistory('Auto Sheet Maker', 'AutoSheetMaker(' + maintainAspectRatio + ')');
}

function AutoSheetMaker(keepAspect)
{ // keepAspect:Boolean - optional. Default to false.
    // Store the ruler.
    var defaultRulerUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    // Global bleed pixels on all docs.
    bleed = 25;

    // Main master document. =======================================================
    var doc = app.activeDocument;
    var layer = doc.activeLayer;
    // Get the variables to work with.
    var width = doc.width.as('px');
    var height = doc.height.as('px');
    var bounds = app.activeDocument.activeLayer.bounds;
    var layerWidth = bounds[2].as('px') - bounds[0].as('px');
    var layerHeight = bounds[3].as('px') - bounds[1].as('px');
    var resolution = doc.resolution;

    // The saved directory location of the main master document.
    Path = doc.path; // If the document is new there is no doc.path. Error is shown.

    // Main master document workings.
    copy = doc.layerSets["Artwork"].duplicate(); // Duplicate Group layer by name.
    doc.activeLayer = doc.layerSets["Artwork copy"]; // Select Group layer by name.
    doc.activeLayer.merge(); // Merge the selection. In this case, the Group layer.
    var dogbox = 1000; // The region dimensions that's considered in the automation.
    ellipse_selection(height / 2 - dogbox / 2, width / 2 - dogbox / 2, width / 2 + dogbox / 2, height / 2 + dogbox / 2); // Make a circle selection.
    freeTransform(0, 0, width / dogbox * 100, height / dogbox * 100); // Resize selection to fit the whole canvas, ignoring bleeds.
    doc.selection.copy(); // Copy the selection.
    deSelect();
    doc.artLayers.getByName('Artwork copy').remove(); // Delete the Artwork copy layer.

    // =============================================================================

    app.documents.add(width, height, resolution, "layout_debug", NewDocumentMode.CMYK); // Create a new document.

    // Page 3: Third document. =====================================================
    // Note: Need to re-initialize as the variables are referencing to the previous document.
    //       Error shown: The requested action requires that the target document is the frontmost document.
    var doc = app.activeDocument;
    var layer = doc.activeLayer;
    // Get the variables to work with.
    var width = doc.width.as('px');
    var height = doc.height.as('px');
    var bounds = app.activeDocument.activeLayer.bounds;
    var layerWidth = bounds[2].as('px') - bounds[0].as('px');
    var layerHeight = bounds[3].as('px') - bounds[1].as('px');
    var resolution = doc.resolution;

    // Create bleed guides.
    doc.guides.add(Direction.HORIZONTAL, bleed);
    doc.guides.add(Direction.VERTICAL, bleed);
    doc.guides.add(Direction.HORIZONTAL, height - bleed);
    doc.guides.add(Direction.VERTICAL, width - bleed);

    /////////////////////////////////////////////////////////////////////////////////////
    // new: creating a group to contain all the layers
    var layersGroup = doc.layerSets.add();

    // Third auto-page workings.
    // x1 biggest.
    doc.paste(); // Paste the selection.
    doc.activeLayer.resize(50, 50, AnchorPosition.MIDDLECENTER); // Resize the pasted layer by 50%.
    doc.activeLayer.translate(0 - width / 4, 0 - height / 4); // Move layer to the top left corner. First copy.

    doc.activeLayer.move(layersGroup, ElementPlacement.INSIDE); // new: adding active layer to the group

    var big_copy = doc.activeLayer; // Copy the selection.
    // x4 mid sized.
    for (var i = 0; i < 2; i++)
    {
        for (var j = 0; j < 2; j++)
        {
            copy = doc.activeLayer.duplicate();
            copy.translate(0 + width / 4 * i, (0 + height / 2) + (height / 4 * j));
            copy.resize(50, 50, AnchorPosition.TOPLEFT);
            copy.move(layersGroup, ElementPlacement.INSIDE); // new: adding active layer to the group
        }
    }
    // Many minis.
    for (var i = 0; i < 3; i++)
    {
        for (var j = 0; j < 6; j++)
        {
            copy = big_copy.duplicate();
            copy.translate((0 + width / 2) + (width / 2 / 3 * i), 0 + (height / 6 * j));
            copy.resize(100 / 3, 100 / 3, AnchorPosition.TOPLEFT);
            copy.move(layersGroup, ElementPlacement.INSIDE); // new: adding active layer to the group
        }
    }

    doc.artLayers.getByName('Background').remove(); // Delete the Background layer.

    /////////////////////////////////////////////////////////////////////////////////////
    // new: selecting the group and merging it down
    doc.activeLayer = layersGroup;
    mergeDown();

    /////////////////////////////////////////////////////////////////////////////////////
    // new: getting coords of the resulting merge layer to transform those
    var bounds = app.activeDocument.activeLayer.bounds;
    var layerWidth = bounds[2].as('px') - bounds[0].as('px');
    var layerHeight = bounds[3].as('px') - bounds[1].as('px');

    freeTransform((-bounds[0].as('px') + bleed), (-bounds[1].as('px') + bleed), (layerWidth - bleed * 2) / layerWidth * 100, (layerHeight - bleed * 2) / layerHeight * 100); // Resize selection within bleeds.

    // =============================================================================

    // Restore the ruler.
    app.preferences.rulerUnits = defaultRulerUnits;

    alert("Script automation completed!");
}

// Ellipse marquee selection.
function ellipse_selection(top, left, right, bottom)
{
    var id1 = charIDToTypeID("setd");
    var desc1 = new ActionDescriptor();
    var id2 = charIDToTypeID("null");
    var ref1 = new ActionReference();
    var id3 = charIDToTypeID("Chnl");
    var id4 = charIDToTypeID("fsel");
    ref1.putProperty(id3, id4);
    desc1.putReference(id2, ref1);
    var id5 = charIDToTypeID("T   ");
    var desc2 = new ActionDescriptor();
    var id6 = charIDToTypeID("Top ");
    var id7 = charIDToTypeID("#Pxl");
    desc2.putUnitDouble(id6, id7, top); //top
    var id8 = charIDToTypeID("Left");
    var id9 = charIDToTypeID("#Pxl");
    desc2.putUnitDouble(id8, id9, left); //left
    var id10 = charIDToTypeID("Btom");
    var id11 = charIDToTypeID("#Pxl");
    desc2.putUnitDouble(id10, id11, bottom); //bottom
    var id12 = charIDToTypeID("Rght");
    var id13 = charIDToTypeID("#Pxl");
    desc2.putUnitDouble(id12, id13, right); //right
    var id14 = charIDToTypeID("Elps");
    desc1.putObject(id5, id14, desc2);
    var id15 = charIDToTypeID("AntA");
    desc1.putBoolean(id15, true);
    executeAction(id1, desc1, DialogModes.NO);
}
// Deselect selection.
function deSelect()
{
    var idsetd = charIDToTypeID("setd");
    var desc19 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref5 = new ActionReference();
    var idChnl = charIDToTypeID("Chnl");
    var idfsel = charIDToTypeID("fsel");
    ref5.putProperty(idChnl, idfsel);
    desc19.putReference(idnull, ref5);
    var idT = charIDToTypeID("T   ");
    var idOrdn = charIDToTypeID("Ordn");
    var idNone = charIDToTypeID("None");
    desc19.putEnumerated(idT, idOrdn, idNone);
    executeAction(idsetd, desc19, DialogModes.NO);
}
// Free transform ('Ctrl' + 't' method).
function freeTransform(top, left, width, height)
{
    var idTrnf = charIDToTypeID("Trnf");
    var desc92 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref23 = new ActionReference();
    var idLyr = charIDToTypeID("Lyr ");
    var idOrdn = charIDToTypeID("Ordn");
    var idTrgt = charIDToTypeID("Trgt");
    ref23.putEnumerated(idLyr, idOrdn, idTrgt);
    desc92.putReference(idnull, ref23);
    var idFTcs = charIDToTypeID("FTcs");
    var idQCSt = charIDToTypeID("QCSt");
    var idQcsa = charIDToTypeID("Qcsa");
    desc92.putEnumerated(idFTcs, idQCSt, idQcsa);
    var idOfst = charIDToTypeID("Ofst");
    var desc93 = new ActionDescriptor();
    var idHrzn = charIDToTypeID("Hrzn");
    var idPxl = charIDToTypeID("#Pxl");
    desc93.putUnitDouble(idHrzn, idPxl, top);
    var idVrtc = charIDToTypeID("Vrtc");
    var idPxl = charIDToTypeID("#Pxl");
    desc93.putUnitDouble(idVrtc, idPxl, left);
    var idOfst = charIDToTypeID("Ofst");
    desc92.putObject(idOfst, idOfst, desc93);
    var idWdth = charIDToTypeID("Wdth");
    var idPrc = charIDToTypeID("#Prc");
    desc92.putUnitDouble(idWdth, idPrc, height);
    var idHght = charIDToTypeID("Hght");
    var idPrc = charIDToTypeID("#Prc");
    desc92.putUnitDouble(idHght, idPrc, width);
    var idIntr = charIDToTypeID("Intr");
    var idIntp = charIDToTypeID("Intp");
    var idbicubicSharper = stringIDToTypeID("bicubicSharper");
    desc92.putEnumerated(idIntr, idIntp, idbicubicSharper);
    executeAction(idTrnf, desc92, DialogModes.NO);
}
// Merge visible layers.
function mergeVisible()
{
    var idMrgV = charIDToTypeID("MrgV");
    executeAction(idMrgV, undefined, DialogModes.NO);
}

function mergeDown()
{
    var desc = new ActionDescriptor();
    executeAction(charIDToTypeID('Mrg2'), desc, DialogModes.NO);
} // end of mergeDown()


Beantwortet von –
Sergey Kritskiy


Antwort geprüft von –
Pedro (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like