Advertisement
boogalooper

face

Oct 31st, 2020 (edited)
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 KB | None | 0 0
  1. #target photoshop
  2.  
  3. //var targetRGB = 180
  4. var targetRGB = [216, 174, 154]
  5.  
  6. if ((new AM('application')).getProperty('numberOfDocuments')) {
  7. app.activeDocument.suspendHistory('Get face bounds', 'function blankState () {return}')
  8. app.activeDocument.suspendHistory('Measure face', 'getFaceBounds()')
  9. app.activeDocument.suspendHistory('Make curves', 'makeCurves()')
  10. }
  11.  
  12. function getFaceBounds() {
  13. var lr = new AM('layer'),
  14. doc = new AM('document');
  15.  
  16. doc.flatten()
  17. lr.copyToNewLayer()
  18. lr.liquify()
  19. doc.fade('difference', 100)
  20. lr.levels(0, 2)
  21. doc.selectRGBChannel()
  22. lr.deleteCurrentLayer()
  23. }
  24.  
  25. function makeCurves() {
  26. var doc = new AM('document');
  27. if (doc.hasProperty('selection') && doc.getProperty('mode')[1] == 'RGBColor') {
  28.  
  29. var midtone = []
  30. if (targetRGB instanceof Array) {
  31. var ch = new AM('channel')
  32. for (var i = 0; i < 3; i++) {
  33. midtone.push(middleToneGamma(targetRGB[i], getMidTone(ch.getProperty('histogram', i+1, true))))
  34. }
  35. } else {
  36. midtone.push(middleToneGamma(targetRGB, getMidTone(doc.getProperty('histogram'))))
  37. }
  38.  
  39. doc.deselect()
  40. doc.stepBack()
  41. doc.makeCurves(128, midtone)
  42.  
  43. } else { doc.stepBack() }
  44. }
  45.  
  46. function middleToneGamma(x2, x1) {
  47. return Math.pow(128 / 255, 1 / (Math.log(x1 / 255) / Math.log(x2 / 255))) * 255
  48. }
  49.  
  50. function getMidTone(h) {
  51. var pixels = 0,
  52. sum = 0;
  53.  
  54. for (var i = 0; i < h.count; i++) {
  55. var key = h.getInteger(i)
  56. pixels += key
  57. sum += key * i
  58. }
  59.  
  60. $.writeln(sum / pixels)
  61. return sum / pixels
  62. }
  63.  
  64. function AM(target) {
  65. var s2t = stringIDToTypeID,
  66. t2s = typeIDToStringID;
  67.  
  68. target = s2t(target)
  69.  
  70. this.getProperty = function (property, id, idxMode) {
  71. property = s2t(property);
  72. (r = new ActionReference()).putProperty(s2t('property'), property);
  73. id ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id))
  74. : r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
  75. return getDescValue(executeActionGet(r), property)
  76. }
  77.  
  78. this.hasProperty = function (property, id, idxMode) {
  79. property = s2t(property);
  80. (r = new ActionReference()).putProperty(s2t('property'), property);
  81. id ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id))
  82. : r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
  83. return executeActionGet(r).hasKey(property)
  84. }
  85.  
  86. this.descToObject = function (d) {
  87. var o = {}
  88. for (var i = 0; i < d.count; i++) {
  89. var k = d.getKey(i)
  90. o[t2s(k)] = getDescValue(d, k)
  91. }
  92. return o
  93. }
  94.  
  95. switch (t2s(target)) {
  96. case 'document':
  97. this.flatten = function () {
  98. executeAction(s2t('flattenImage'), undefined, DialogModes.NO);
  99. }
  100.  
  101. this.fade = function (mode, opacity) {
  102. (d = new ActionDescriptor()).putUnitDouble(s2t('opacity'), s2t('percentUnit'), opacity);
  103. d.putEnumerated(s2t('mode'), s2t('blendMode'), s2t(mode));
  104. executeAction(s2t('fade'), d, DialogModes.NO);
  105. }
  106.  
  107. this.selectRGBChannel = function () {
  108. (r = new ActionReference()).putProperty(s2t('channel'), s2t('selection'));
  109. (d = new ActionDescriptor()).putReference(s2t('null'), r);
  110. (r1 = new ActionReference()).putEnumerated(s2t('channel'), s2t('channel'), s2t('RGB'));
  111. d.putReference(s2t('to'), r1);
  112. executeAction(s2t('set'), d, DialogModes.NO);
  113. }
  114.  
  115. this.stepBack = function () {
  116. (r = new ActionReference()).putEnumerated(charIDToTypeID('HstS'), s2t('ordinal'), s2t('previous'));
  117. (d = new ActionDescriptor()).putReference(s2t('null'), r);
  118. executeAction(s2t('select'), d, DialogModes.NO);
  119. }
  120.  
  121. this.deselect = function () {
  122. (r = new ActionReference()).putProperty(s2t('channel'), s2t('selection'));
  123. (d = new ActionDescriptor()).putReference(s2t('null'), r);
  124. d.putEnumerated(s2t('to'), s2t('ordinal'), s2t('none'));
  125. executeAction(s2t('set'), d, DialogModes.NO);
  126. }
  127.  
  128. this.makeCurves = function (from, to) {
  129. (r = new ActionReference()).putClass(s2t('adjustmentLayer'));
  130. (d = new ActionDescriptor()).putReference(s2t('null'), r);
  131. (d2 = new ActionDescriptor()).putEnumerated(s2t('presetKind'), s2t('presetKindType'), s2t('presetKindDefault'));
  132. (d1 = new ActionDescriptor()).putObject(s2t('type'), s2t('curves'), d2);
  133. d.putObject(s2t('using'), s2t('adjustmentLayer'), d1);
  134. executeAction(s2t('make'), d, DialogModes.NO);
  135.  
  136. (r = new ActionReference()).putEnumerated(s2t('adjustmentLayer'), s2t('ordinal'), s2t('targetEnum'));
  137. (d = new ActionDescriptor()).putReference(s2t('null'), r);
  138. (d1 = new ActionDescriptor()).putEnumerated(s2t('presetKind'), s2t('presetKindType'), s2t('presetKindCustom'));
  139.  
  140. var mode = to.length == 1 ? ['composite'] : ['red', 'green', 'blue'],
  141. l = new ActionList();
  142.  
  143. for (var i = 0; i < to.length; i++) {
  144. (r1 = new ActionReference()).putEnumerated(s2t('channel'), s2t('channel'), s2t(mode[i]));
  145. (d2 = new ActionDescriptor()).putReference(s2t('channel'), r1);
  146. (d3 = new ActionDescriptor()).putDouble(s2t('horizontal'), 0);
  147. d3.putDouble(s2t('vertical'), 0);
  148. (l1 = new ActionList()).putObject(s2t('Pnt '), d3);
  149. (d4 = new ActionDescriptor()).putDouble(s2t('horizontal'), from);
  150. d4.putDouble(s2t('vertical'), to[i]);
  151. l1.putObject(s2t('Pnt '), d4);
  152. (d5 = new ActionDescriptor()).putDouble(s2t('horizontal'), 255);
  153. d5.putDouble(s2t('vertical'), 255);
  154. l1.putObject(s2t('Pnt '), d5);
  155. d2.putList(s2t('curve'), l1);
  156. l.putObject(s2t('curvesAdjustment'), d2);
  157. }
  158.  
  159. d1.putList(s2t('adjustment'), l);
  160. d.putObject(s2t('to'), s2t('curves'), d1);
  161. executeAction(s2t('set'), d, DialogModes.NO);
  162. }
  163.  
  164. break;
  165. case 'layer':
  166. this.liquify = function () {
  167. var mesh = String.fromCharCode(0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 0, 0, 8, 102, 97, 99, 101, 77, 101, 115, 104, 0, 0, 0, 3, 0, 0, 0, 21, 102, 97, 99, 101, 68, 101, 115, 99, 114, 105, 112, 116, 111, 114, 86, 101, 114, 115, 105, 111, 110, 108, 111, 110, 103, 0, 0, 0, 2, 0, 0, 0, 15, 102, 97, 99, 101, 77, 101, 115, 104, 86, 101, 114, 115, 105, 111, 110, 108, 111, 110, 103, 0, 0, 0, 2, 0, 0, 0, 12, 102, 97, 99, 101, 73, 110, 102, 111, 76, 105, 115, 116, 86, 108, 76, 115, 0, 0, 0, 1, 79, 98, 106, 99, 0, 0, 0, 1, 0, 0, 0, 0, 0, 8, 102, 97, 99, 101, 73, 110, 102, 111, 0, 0, 0, 3, 0, 0, 0, 10, 102, 97, 99, 101, 67, 101, 110, 116, 101, 114, 79, 98, 106, 99, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 110, 117, 108, 108, 0, 0, 0, 2, 0, 0, 0, 0, 88, 32, 32, 32, 100, 111, 117, 98, 63, 228, 166, 26, 13, 248, 61, 51, 0, 0, 0, 0, 89, 32, 32, 32, 100, 111, 117, 98, 63, 203, 48, 176, 9, 117, 167, 81, 0, 0, 0, 13, 102, 101, 97, 116, 117, 114, 101, 86, 97, 108, 117, 101, 115, 79, 98, 106, 99, 0, 0, 0, 1, 0, 0, 0, 0, 0, 13, 102, 101, 97, 116, 117, 114, 101, 86, 97, 108, 117, 101, 115, 0, 0, 0, 1, 0, 0, 0, 9, 110, 111, 115, 101, 87, 105, 100, 116, 104, 100, 111, 117, 98, 191, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 102, 101, 97, 116, 117, 114, 101, 68, 105, 115, 112, 108, 97, 99, 101, 109, 101, 110, 116, 115, 79, 98, 106, 99, 0, 0, 0, 1, 0, 0, 0, 0, 0, 20, 102, 101, 97, 116, 117, 114, 101, 68, 105, 115, 112, 108, 97, 99, 101, 109, 101, 110, 116, 115, 0, 0, 0, 0);
  168. (d = new ActionDescriptor()).putData(s2t('faceMeshData'), mesh);
  169. executeAction(charIDToTypeID('LqFy'), d, DialogModes.NO);
  170. }
  171.  
  172. this.levels = function (begin, end) {
  173. (d = new ActionDescriptor()).putEnumerated(s2t('presetKind'), s2t('presetKindType'), s2t('presetKindCustom'));
  174. (r = new ActionReference()).putEnumerated(s2t('channel'), s2t('channel'), s2t('composite'));
  175. (d1 = new ActionDescriptor()).putReference(s2t('channel'), r);
  176. (l = new ActionList()).putInteger(begin);
  177. l.putInteger(end);
  178. d1.putList(s2t('input'), l);
  179. (l1 = new ActionList()).putObject(s2t('levelsAdjustment'), d1);
  180. d.putList(s2t('adjustment'), l1);
  181. executeAction(s2t('levels'), d, DialogModes.NO);
  182. }
  183.  
  184. this.copyToNewLayer = function () {
  185. executeAction(s2t("copyToLayer"), undefined, DialogModes.NO);
  186. }
  187.  
  188. this.deleteCurrentLayer = function () {
  189. (r = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
  190. (d = new ActionDescriptor()).putReference(s2t("null"), r);
  191. executeAction(s2t("delete"), d, DialogModes.NO);
  192. }
  193. break;
  194. }
  195.  
  196. function getDescValue(d, p) {
  197. switch (d.getType(p)) {
  198. case DescValueType.OBJECTTYPE: return (d.getObjectValue(p));
  199. case DescValueType.LISTTYPE: return d.getList(p);
  200. case DescValueType.REFERENCETYPE: return d.getReference(p);
  201. case DescValueType.BOOLEANTYPE: return d.getBoolean(p);
  202. case DescValueType.STRINGTYPE: return d.getString(p);
  203. case DescValueType.INTEGERTYPE: return d.getInteger(p);
  204. case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(p);
  205. case DescValueType.DOUBLETYPE: return d.getDouble(p);
  206. case DescValueType.ALIASTYPE: return d.getPath(p);
  207. case DescValueType.CLASSTYPE: return d.getClass(p);
  208. case DescValueType.UNITDOUBLE: return (d.getUnitDoubleValue(p));
  209. case DescValueType.ENUMERATEDTYPE: return [t2s(d.getEnumerationType(p)), t2s(d.getEnumerationValue(p))];
  210. default: break;
  211. };
  212. }
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement