You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Photoshop多图层缩放脚本精度异常原因排查求助

问题分析与解决方案

偏差原因

  1. 图层边界精度问题:selectedLayer.bounds返回的是图层在文档中的可视边界,该值会受图层非整数坐标、Photoshop像素对齐设置影响,导致计算出的宽/高并非图层内容的精确原始尺寸,以此为基础计算缩放比例会引入误差。
  2. 像素网格对齐干扰:默认状态下,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) {
        // 保存当前像素对齐设置
        var originalSnapToPixels = preferences.snapToPixels;
        // 临时关闭像素对齐,避免缩放时自动取整
        preferences.snapToPixels = false;

        // Target scale percentage
        var scalePercentage = 76.39;

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

            // 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: use direct percentage, lock aspect ratio implicitly
            desc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));
            // 直接设置目标缩放百分比,宽高使用相同值保证比例锁定
            desc.putUnitDouble(charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), scalePercentage);
            desc.putUnitDouble(charIDToTypeID('Hght'), charIDToTypeID('#Prc'), scalePercentage);
            desc.putEnumerated(charIDToTypeID('Intr'), charIDToTypeID('Intp'), charIDToTypeID('Bcbc'));

            // Execute transformation
            executeAction(charIDToTypeID('Trnf'), desc, DialogModes.NO);
        }

        // 恢复原始像素对齐设置
        preferences.snapToPixels = originalSnapToPixels;
    } else {
        alert("Please select one or more layers.");
    }
}

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

关键修改点

  • 移除不必要的边界计算:直接使用目标缩放百分比scalePercentage,避免基于bounds的误差引入。
  • 临时关闭像素对齐:保存原始的snapToPixels设置,变换前关闭、变换后恢复,确保Photoshop不会自动调整缩放后的尺寸到像素网格。
  • 直接指定缩放比例:在ActionDescriptor中直接传入目标百分比,宽高使用相同值,Photoshop会自动保持图层的宽高比。

内容的提问来源于stack exchange,提问作者Pubg Mobile

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 16:57:51