-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_crop4paste.jsx
197 lines (177 loc) · 5.79 KB
/
face_crop4paste.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// This program will run on Photoshop CC 2015.5 and later
// Program to swap faces from two files given the coordinates of the faces
// The source and target should be opened in Photoshop
// The coordinates are given in the form of a JSON file
// The JSON file should be in the following format:
// {
// "Panorama_auto_111_prim_1": [
// {
// "source": {
// "photo": "Panorama_auto_111_prim_1",
// "facial_area": {
// "x": 5511,
// "y": 881,
// "w": 134,
// "h": 172
// }
// },
// "destiny": {
// "photo": "Panorama_auto_111_prim_7",
// "facial_area": {
// "x": 600,
// "y": 280,
// "w": 136,
// "h": 164
// }
// }
// }
// ],
// "Panorama_auto_111_prim_3": [
// {
// "source": {
// "photo": "Panorama_auto_111_prim_3",
// "facial_area": {
// "x": 1740,
// "y": 653,
// "w": 148,
// "h": 197
// }
// },
// "destiny": {
// "photo": "Panorama_auto_111_prim_7",
// "facial_area": {
// "x": 3327,
// "y": 902,
// "w": 148,
// "h": 196
// }
// }
// },
// {
// "source": {
// "photo": "Panorama_auto_111_prim_3",
// "facial_area": {
// "x": 1740,
// "y": 653,
// "w": 148,
// "h": 197
// }
// },
// "destiny": {
// "photo": "Panorama_auto_111_prim_7",
// "facial_area": {
// "x": 3235,
// "y": 150,
// "w": 139,
// "h": 174
// }
// }
// }
// ]
// }
#target photoshop;
// Include the json parser for Photoshop
#include "PhotoshopJsonParser.js";
// Set the ruler units to pixels
app.preferences.rulerUnits = Units.PIXELS;
// Set the layer name from where the face will be copied
var layer2Copy = "Pano";
// Function to read the JSON file
function readJSONFile(file) {
var jsonFile = new File(file);
jsonFile.open('r');
var str = jsonFile.read();
jsonFile.close();
return JSON.parse(str);
}
// Function to copy the face from the source area
function copyFace(actDoc, sourceArea) {
// Select the layer Pano
var layerName = layer2Copy;
actDoc.activeLayer = app.activeDocument.layers.getByName(layerName);
// Select the area of the face
var sourceBounds = sourceArea;
var sourceX = sourceBounds.x;
var sourceY = sourceBounds.y;
var sourceW = sourceBounds.w;
var sourceH = sourceBounds.h;
var sourceBounds = [ [sourceX, sourceY],
[sourceX, sourceY + sourceH],
[sourceX + sourceW, sourceY + sourceH],
[sourceX + sourceW, sourceY]
];
actDoc.selection.select(sourceBounds);
actDoc.selection.copy();
return actDoc;
}
// Function to paste the face in the destiny area
function pasteFace(actDoc, destinyArea) {
// Create a new layer on top
var layer = actDoc.artLayers.add();
layer.name = "Face";
// Select the new layer
actDoc.activeLayer = layer;
// Paste the face
actDoc.paste();
// Translate the face to the destiny area
var layer = actDoc.activeLayer;
var boundX = layer.bounds[0] * -1;
var boundY = layer.bounds[1] * -1;
// alert('boundX: ' + boundX + ', boundY: ' + boundY);
var destinyBounds = destinyArea;
var destinyX = destinyBounds.x;
var destinyY = destinyBounds.y;
layer.translate(boundX + destinyX, boundY + destinyY);
return actDoc;
}
// Function to select the open document by name
function selectOpenDocument(name) {
for (var i = 0; i < app.documents.length; i++) {
if (app.documents[i].name == name) {
app.activeDocument = app.documents[i];
return app.activeDocument;
}
}
alert("The document " + name + " is not open.");
// Exit the program
exit();
return null;
}
// Main function
function main() {
var jsonFile = File.openDialog("Please select the JSON file.", "*.json", false);
// alert('jsonFile: ' + jsonFile);
var json = readJSONFile(jsonFile);
actDoc = app.activeDocument;
for (var key in json) {
// alert('key: ' + key);
var faces = json[key];
for (var i = 0; i < faces.length; i++) {
var face = faces[i];
// alert('face.source.photo: ' + face.source.photo);
var actDoc = selectOpenDocument(face.source.photo + ".psd");
copyFace(actDoc, face.source.facial_area);
// alert('face.destiny.photo: ' + face.destiny.photo);
var actDoc = selectOpenDocument(face.destiny.photo + ".psd");
pasteFace(actDoc, face.destiny.facial_area);
}
}
// Create a group of the layers in the active document
var group = actDoc.layerSets.add();
group.name = "Faces";
var face_count = 0;
var exist_Face_layer = true;
while (exist_Face_layer) {
try {
var layer = actDoc.layers.getByName("Face");
if (layer) {
face_count++;
layer.name = "Face_" + face_count;
layer.move(group, ElementPlacement.INSIDE);
}
} catch (e) {
exist_Face_layer = false;
}
}
}
main();