通过脚本更改多层缩放尺寸

问题描述 投票:0回答:1

我编写了以下 Photoshop 脚本来更改多层比例大小:

// 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.");
}

上面的脚本运行良好,但没有提供准确的结果。例如,预计将宽度 (W) 和高度 (H) 更改为 76.39% 的缩放尺寸。然而,在测试过程中,我注意到宽度更改为 76.92%,高度更改为 75.76%。

什么可能导致脚本中出现这种差异?值得注意的是,保持纵横比至关重要。

javascript jsx photoshop photoshop-script
1个回答
0
投票

举一个简单的例子,使用这个 3 x 4 像素图像:

如果将其放大 150%,您将得到 4.5 x 6 像素的大小。但 Photoshop 无法处理部分像素,因此它会对缩放后的图像进行抗锯齿处理,并总共使用 5 x 6 像素(或 166.67% x 150%)

© www.soinside.com 2019 - 2024. All rights reserved.