吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1137|回复: 31
收起左侧

[其他转载] Photoshop参考线切片导出

  [复制链接]
zhengzhenhui945 发表于 2025-3-24 22:40
本帖最后由 zhengzhenhui945 于 2025-3-30 22:40 编辑

其主要用途是对当前在 Photoshop 中打开的 PSD 文档进行切片处理,并将切片后的图像自动保存到脚本的文件夹中

ps里面也有这样的,但是导出太麻烦,所以写了一个直接拖动切片的,输出结构【模版名/模版名+数字递增.jpg】导出格式可以自己代码里面改
.
[JavaScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 直接处理当前打开的文档
function sliceCurrentDocument() {
    var doc = activeDocument;
    if (!doc) {
        alert("没有打开的文档!");
        return;
    }
 
    // 获取脚本所在的目录作为主文件夹
    var mainFolder = new Folder($.fileName).parent;
    var outputFolder = new Folder(mainFolder.fsName + "/输出文件夹");
    if (!outputFolder.exists) {
        if (!outputFolder.create()) {
            alert("无法创建输出文件夹: " + outputFolder.fsName);
            return;
        }
    }
 
    var psdName = doc.name.replace('.psd', '');
    // 创建以PSD文件名命名的子文件夹
    var psdOutputFolder = new Folder(outputFolder.fsName + "/" + psdName);
    if (!psdOutputFolder.exists) {
        if (!psdOutputFolder.create()) {
            alert("无法创建PSD输出文件夹: " + psdOutputFolder.fsName);
            return;
        }
    }
 
    // 假设默认导出格式为JPG,可按需修改
    var exportFormat = "JPG";
 
    // 切片并保存处理后的文件,文件名格式为【PSD名+排序】
    sliceAndSave(doc, psdOutputFolder, psdName, exportFormat);
 
    // 显示处理完成信息的对话框
    alert("处理完成!");
}
 
// 切片并保存处理后的文件
function sliceAndSave(doc, targetFolder, baseFileName, exportFormat) {
    var horizontalGuides = [0];
    var verticalGuides = [0];
    var sliceIndex = 0;
 
    // 收集水平和垂直参考线
    for (var i = 0; i < doc.guides.length; i++) {
        if (doc.guides[i].direction.typename === "Direction.HORIZONTAL") {
            horizontalGuides.push(doc.guides[i].coordinate);
        } else {
            verticalGuides.push(doc.guides[i].coordinate);
        }
    }
 
    var historyState = activeDocument.activeHistoryState; // 记录历史状态
 
    // 遍历水平和垂直参考线进行切片
    for (var y = 0; y < horizontalGuides.length; y++) {
        var bottom = (y < horizontalGuides.length - 1) ? horizontalGuides[y + 1] : doc.height;
        for (var x = 0; x < verticalGuides.length; x++) {
            sliceIndex++;
            var right = (x < verticalGuides.length - 1) ? verticalGuides[x + 1] : doc.width;
 
            // 裁剪文档
            doc.crop([verticalGuides[x], horizontalGuides[y], right, bottom]);
 
            // 构造文件名
            var fileName = baseFileName + "-" + sliceIndex + "." + exportFormat.toLowerCase();
            var file = new File(targetFolder.fsName + "/" + fileName);
 
            // 保存文件
            if (exportFormat === "JPG") {
                var jpgSaveOptions = new JPEGSaveOptions();
                jpgSaveOptions.quality = 10;
                doc.saveAs(file, jpgSaveOptions, true, Extension.LOWERCASE);
            } else if (exportFormat === "PNG") {
                var pngSaveOptions = new PNGSaveOptions();
                pngSaveOptions.compression = 0;
                doc.saveAs(file, pngSaveOptions, true, Extension.LOWERCASE);
            } else if (exportFormat === "PSD") {
                var psdSaveOptions = new PhotoshopSaveOptions();
                doc.saveAs(file, psdSaveOptions, true, Extension.LOWERCASE);
            }
 
            // 恢复文档到原始状态
            activeDocument.activeHistoryState = historyState;
        }
    }
}
 
// 调用主函数
sliceCurrentDocument();

使用方法,粘贴到txt文档里面,另存为.js格式,拖动到PS里面即可运行
==================================
2025年3月30日 更新可视化面板导出,支持格式多选导出

界面

界面



Photoshop参考线切片导出-V2.zip

1.98 KB, 下载次数: 3, 下载积分: 吾爱币 -1 CB

免费评分

参与人数 4吾爱币 +3 热心值 +4 收起 理由
zzkings + 1 我很赞同!
jiaguorui0 + 1 + 1 谢谢@Thanks!
surepj + 1 + 1 用心讨论,共获提升!
chinawolf2000 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

林泽西 发表于 2025-3-25 10:11
本帖最后由 林泽西 于 2025-3-25 10:25 编辑

觉得过于鸡肋呢……参考线直接就能切  而且都带顺序数字排列的……还可以调整大小和后缀格式呢

这是提取所有图层的代码生成后……
12.png
11.png
 楼主| zhengzhenhui945 发表于 2025-3-25 00:30
Amo52 发表于 2025-3-25 00:04
PS不是自带按参考线切片么。。。

是PS里面有,我上有说,可以输出文件夹自己归类【模版名+数字递增】
 楼主| zhengzhenhui945 发表于 2025-3-24 22:48
这个是提取所有图层,依次自适应裁剪到每个图层,并导出PNG
[JavaScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
var doc = app.activeDocument
var docName = doc.name.substring(0 , doc.name.lastIndexOf('.'))
var saveOptions = PNGSaveOptions//PNG格式保存
saveOptions.compression = true//压缩
var historyState = activeDocument.activeHistoryState;
for(var i=0 ; i<doc.layers.length ; i++){
    doc.crop(doc.layers[i].bounds)
    var file = new File(decodeURI(doc.path)+"/"+docName+"-"+doc.layers[i].name+".png")//保存路径
    doc.saveAs(file, saveOptions, true, Extension.NONE);
}
activeDocument.activeHistoryState = historyState;

免费评分

参与人数 2吾爱币 +3 热心值 +1 收起 理由
Furutsukl + 1 + 1 谢谢@Thanks!
songbing490 + 2 热心回复!

查看全部评分

WORSONG178 发表于 2025-3-24 23:02
倒是没试过需要这种功能。
Amo52 发表于 2025-3-25 00:04
PS不是自带按参考线切片么。。。
woyouhuilaile 发表于 2025-3-25 00:48
这个有点意思 应该提高效率
艺达 发表于 2025-3-25 08:25
PS切片干啥用的??学了二十年没用过。
zlqhysy 发表于 2025-3-25 08:36
艺达 发表于 2025-3-25 08:25
PS切片干啥用的??学了二十年没用过。

有同问,没用过
zwm5646 发表于 2025-3-25 08:56
是否可以按调节尺寸来切片的?比如从左到右?
tezhuo 发表于 2025-3-25 09:01
大神厉害,这都能行,以前都是手动操作,真的才知道PS也能用JS脚本
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-23 20:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表