Pubg Mobile
Pubg Mobile

Reputation: 765

Change multiple layers scale size by script

I wrote following photoshop script for change multiple layers scale size:

// Function to resize the selected layers
function resizeSelectedLayers() {
    var doc = app.activeDocument;
    var selectedLayers = [];

    // Prompt user to select layers
    var selectedLayerIndices = prompt("Enter the indices of the layers you want to resize (comma-separated):", "");
    if (selectedLayerIndices === null || selectedLayerIndices === "") {
        alert("No layers selected.");
        return;
    }

    // Convert comma-separated indices to array
    var indicesArray = selectedLayerIndices.split(",");
    for (var i = 0; i < indicesArray.length; i++) {
        var index = parseInt(indicesArray[i]);
        if (!isNaN(index) && index >= 0 && index < doc.layers.length) {
            selectedLayers.push(doc.layers[index]);
        } else {
            alert("Invalid layer index: " + indicesArray[i]);
            return;
        }
    }

    // Check if there are selected layers
    if (selectedLayers.length > 0) {
        // Loop through each selected layer
        for (var j = 0; j < selectedLayers.length; j++) {
            var selectedLayer = selectedLayers[j];
            doc.activeLayer = selectedLayer;

            // Calculate scaling factor
            var scalePercentage = 76.39; // Percentage size
            var scaleFactor = scalePercentage / 100;

            // Get current dimensions
            var width = selectedLayer.bounds[2] - selectedLayer.bounds[0];
            var height = selectedLayer.bounds[3] - selectedLayer.bounds[1];

            // Calculate new dimensions with locked aspect ratio
            var newWidth = width * scaleFactor;
            var newHeight = height * scaleFactor;

            // Calculate the scaling ratio
            var ratio = newWidth / width;

            // Set up the transformation
            var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
            desc.putReference(charIDToTypeID('null'), ref);

            // Set transformation parameters
            desc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));
            desc.putUnitDouble(charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), 100 * ratio);
            desc.putUnitDouble(charIDToTypeID('Hght'), charIDToTypeID('#Prc'), 100 * ratio);
            desc.putEnumerated(charIDToTypeID('Intr'), charIDToTypeID('Intp'), charIDToTypeID('Bcbc'));

            // Execute transformation
            executeAction(charIDToTypeID('Trnf'), desc, DialogModes.NO);
        }
    } else {
        alert("Please select one or more layers.");
    }
}

// Check if there is an active document
if (app.documents.length > 0) {
    // Call the resizeSelectedLayers function
    resizeSelectedLayers();
} else {
    alert("No documents are open.");
}

The script above is functioning well, but it doesn't provide accurate results. For instance, it's expected to change both the width (W) and height (H) to a scale size of 76.39%. However, during testing, I noticed that the width changed to 76.92% and the height changed to 75.76%.

What could be causing this discrepancy in the script? It's worth noting that maintaining the aspect ratio is essential.

Upvotes: 0

Views: 95

Answers (1)

cybernetic.nomad
cybernetic.nomad

Reputation: 6418

To take a simple example, take this 3 x 4 pixel image:

enter image description here

If you scale it up 150%, you would get a size of 4.5 x 6 pixels. But Photoshop can't do partial pixels, so it anti-aliases the scaled image and uses a total of 5 x 6 pixels (or 166.67% x 150%)

enter image description here

Upvotes: 0

Related Questions