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

Photoshop脚本问题:矩形选区转椭圆选区位置与尺寸异常

解决Photoshop脚本中矩形转椭圆选区的尺寸与位置偏移问题

问题根源

你编写的脚本出现选区偏移、尺寸缩小的核心原因是单位不匹配:

  • app.activeDocument.selection.bounds返回的是基于像素的选区边界坐标
  • 但脚本中executeAction使用distanceUnit作为单位时,会按照当前文档的标尺单位(如厘米、英寸)解析数值,导致坐标被错误转换,最终出现偏移和尺寸错误。

修正方案

将distanceUnit替换为pixelUnit,让参数单位与selection.bounds的像素单位完全匹配,即可解决问题。

修正后的代码

// 获取当前选区的像素边界
var selBounds = app.activeDocument.selection.bounds;

// 构建ActionReference与ActionDescriptor
var NAR = new ActionReference();
NAR.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));

var NAD = new ActionDescriptor();
NAD.putReference(stringIDToTypeID("null"), NAR);

var NAD2 = new ActionDescriptor();
// 使用pixelUnit匹配选区边界的像素单位
NAD2.putUnitDouble(stringIDToTypeID("top"), stringIDToTypeID("pixelUnit"), selBounds[1]);
NAD2.putUnitDouble(stringIDToTypeID("left"), stringIDToTypeID("pixelUnit"), selBounds[0]);
NAD2.putUnitDouble(stringIDToTypeID("bottom"), stringIDToTypeID("pixelUnit"), selBounds[3]);
NAD2.putUnitDouble(stringIDToTypeID("right"), stringIDToTypeID("pixelUnit"), selBounds[2]);

NAD.putObject(stringIDToTypeID("to"), stringIDToTypeID("ellipse"), NAD2);
NAD.putBoolean(stringIDToTypeID("antiAlias"), true);

executeAction(stringIDToTypeID("set"), NAD, DialogModes.NO);

补充说明

如果需要适配非像素的文档标尺单位,可以通过文档分辨率转换坐标:
例如将像素值转为英寸单位:像素值 / app.activeDocument.resolution,再将pixelUnit改回distanceUnit。但直接使用pixelUnit是最稳定的方案,无需考虑文档分辨率或标尺单位的变化。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 03:05:28