-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_bouding_boxes.jsx
95 lines (81 loc) · 3.13 KB
/
extract_bouding_boxes.jsx
1
2
3
4
5
6
7
8
9
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
92
93
94
95
function getLayerCenter(layer, docWidth, docHeight) {
var bounds = layer.bounds;
var centerX = ((bounds[0].as("px") + bounds[2].as("px")) / 2) / docWidth;
var centerY = ((bounds[1].as("px") + bounds[3].as("px")) / 2) / docHeight;
var width = (bounds[2].as("px") - bounds[0].as("px")) / docWidth;
var height = (bounds[3].as("px") - bounds[1].as("px")) / docHeight;
return {
x: centerX,
y: centerY,
width: width,
height: height
};
}
function processLayer(layer, layerInfo, parentFolderName, docWidth, docHeight) {
if (!layer.isBackgroundLayer && layer.visible) {
var layerCenter = getLayerCenter(layer, docWidth, docHeight);
layerInfo.push({
name: layer.name,
folder: parentFolderName,
centerX: layerCenter.x,
centerY: layerCenter.y,
width: layerCenter.width,
height: layerCenter.height
});
}
}
function processLayers(layers, layerInfo, parentFolderName, docWidth, docHeight) {
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
if (layer.typename === "LayerSet") {
processLayers(layer.layers, layerInfo, layer.name, docWidth, docHeight);
} else {
processLayer(layer, layerInfo, parentFolderName, docWidth, docHeight);
}
}
}
function writeBoudingBoxesToFile(layerInfo, doc) {
var filePath = doc.path.fsName.replace(/\\/g, '/');
var fileName = doc.name.replace(/\.[^\.]+$/, '');
var file = new File(filePath + '/' + fileName + '.txt');
file.encoding = "UTF-8";
file.open("w");
for (var i = 0; i < layerInfo.length; i++) {
var info = layerInfo[i];
var folder = info.folder || "Root";
file.writeln(folder.replace(/\?/g, "") + " " + info.centerX.toFixed(8) + " " + info.centerY.toFixed(8) + " " + info.width.toFixed(8) + " " + info.height.toFixed(8));
}
file.close();
alert("Bounding boxes have been saved to " + fileName + ".txt in the same folder as the open file.");
}
function writeClassesToFile(layerInfo, doc) {
var uniqueFolders = {};
for (var i = 0; i < layerInfo.length; i++) {
var folder = layerInfo[i].folder || "Root";
uniqueFolders[folder.replace(/\?/g, "")] = true;
}
var filePath = doc.path.fsName.replace(/\\/g, '/');
var fileName = doc.name.replace(/\.[^\.]+$/, '');
var file = new File(filePath + '/' + fileName + '_classes.txt');
file.encoding = "UTF-8";
file.open("w");
for (var folder in uniqueFolders) {
file.writeln(folder);
}
file.close();
alert("Class names have been saved to " + fileName + "_classes.txt in the same folder as the open file.");
}
function main() {
if (app.documents.length === 0) {
alert("No active document found. Please open a document.");
return;
}
var doc = app.activeDocument;
var layerInfo = [];
var docWidth = doc.width.as("px");
var docHeight = doc.height.as("px");
processLayers(doc.layers, layerInfo, null, docWidth, docHeight);
writeBoudingBoxesToFile(layerInfo, doc);
writeClassesToFile(layerInfo, doc);
}
main();