Watermark Exif sous Photoshop CS2, nouvelle version

Mise à jour de mon script de watermarking de photo pour Photoshop CS2.
Cette nouvelle version permet de choisir son copyright, la position du cadre, la taille et la police de caractère du texte.
Tout ces choix se font dans une petite interface graphique sans prétention :

Cliquez sur « Lire la suite » pour accéder au code source du script.

  1. // Pour être sur que la fenêtre de photoshop soit active
  2. #target photoshop
  3. app.bringToFront();
  4. // A décommenter si l’on veut déboger le script
  5. /*
  6. debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)
  7. $.level = 0;
  8. debugger; // launch debugger on next line
  9. */
  10. // Pas de question pendant le script
  11. displayDialogs = DialogModes.NO;
  12. // On sauvegarde et met en place les unités de mesure
  13. var defaultRulerUnits = preferences.rulerUnits;
  14. preferences.rulerUnits = Units.PIXELS;
  15. var defaultTypeUnits = preferences.typeUnits;
  16. preferences.typeUnits = TypeUnits.PIXELS;
  17. // Quelques variables fort utiles pour la suite
  18. var AD = activeDocument;
  19. var black = new SolidColor();
  20. black.rgb.red = black.rgb.green = black.rgb.blue = 0;
  21. var white = new SolidColor();
  22. white.rgb.red = white.rgb.green = white.rgb.blue = 255;
  23. backgroundColor = black;
  24. var header =  » 133434 « ;
  25. // Nous allons chercher les infos qui nous interessent dans la liste des infos EXIF
  26. for(n = 0; n < AD.info.exif.length; n = n + 1 ) {
  27.         var stringTemp = AD.info.exif[n][1];
  28.         // J’ai trouvé ces références à coup d’essais, le nom de variable devraient être
  29.         // en toutes lettres, probablement un problème de traduction.
  30.         // Durée d’exposition
  31.         if(stringTemp.indexOf( » 133434″)!=-1){
  32.                 var expTime = stringTemp.substr(header.length,stringTemp.length-header.length);
  33.         }
  34.   // Date de prise de vue
  35.         if(stringTemp.indexOf( » 136867″)!=-1){
  36.                 var phoTime = stringTemp.substr(header.length,stringTemp.length-header.length);
  37.                 var dateArray1 = phoTime.split( » « , 1);
  38.                 phoTime = dateArray1[0];
  39.                 var dateArray2 = phoTime.split(« : »);
  40.                 var monthsArray =[« Janvier », « Février », « Mars », « Avril », « Mai », « Juin », « Juillet », « Août », « Septembre », « Octobre », « Novembre », « Décembre »];
  41.                 phoTime = dateArray2[2] + » « + monthsArray[dateArray2[1]-1]+ » « +dateArray2[0];
  42.         }
  43.   // Modèle d’appareil photo
  44.         if(stringTemp.indexOf( » 100272″)!=-1){
  45.                 var cameraModel = stringTemp.substr(header.length,stringTemp.length-header.length);
  46.         }
  47.   // Ouverture focale
  48.         if(stringTemp.indexOf( » 133437″)!=-1){
  49.                 var FStop = stringTemp.substr(header.length,stringTemp.length-header.length);
  50.         }
  51.   // Règlage ISO
  52.         if(stringTemp.indexOf( » 134855″)!=-1){
  53.                 var ISO = stringTemp.substr(header.length,stringTemp.length-header.length);
  54.         }
  55.         // Longueur focale
  56.         if(stringTemp.indexOf( » 137386″)!=-1){
  57.                 var FLength = stringTemp.substr(header.length,stringTemp.length-header.length);
  58.         }
  59. }
  60. var dlgResource = «  »;
  61.     dlgResource += « dialog{text:’Paramètres Watermark Exif’,bounds:[100,100,440,320], « 
  62.         dlgResource +=  » panel1:Panel{bounds:[20,20,110,105] , text:’Position’ ,\
  63.                          properties:{borderStyle:’etched’,su1PanelCoordinates:true}, « ;
  64.         dlgResource +=  »  group0:Group{bounds:[0,10,100,100], « 
  65.         dlgResource +=  »   radiobutton1:RadioButton{bounds:[10,10,41,31] , text:’1′ }, « 
  66.         dlgResource +=  »   radiobutton2:RadioButton{bounds:[50,10,81,31] , text:’2′ }, « 
  67.         dlgResource +=  »   radiobutton3:RadioButton{bounds:[10,35,41,71] , text:’3′ }, « 
  68.         dlgResource +=  »   radiobutton4:RadioButton{bounds:[50,35,81,71] , text:’4′, value:true} « 
  69.         dlgResource +=  »  } « 
  70.         dlgResource +=  » }, « 
  71.         dlgResource +=  » copyrightInfoSt: StaticText { text:’Copyright Info:’, bounds:[130,20,320,40] }, « 
  72.         dlgResource +=  » copyrightInfoEt: EditText { text:’copyright’, bounds:[130, 42, 320, 62] }, « 
  73.         dlgResource +=  » FlattenCb:     Checkbox { text:’Applatir les couches’, bounds:[130,75,320,95], value:false }, « 
  74.         dlgResource +=  » FontDDLSt: StaticText { text:’Police de caractère:’, bounds:[130,110,320,130] }, « 
  75.         dlgResource +=  » FontDDL: DropDownList { bounds:[130,132,320,152] }, « 
  76.         dlgResource +=  » FontSizeSt: StaticText { text:’Taille de texte:’, bounds:[20,110,120,130] }, « 
  77.         dlgResource +=  » FontSizeDDL: DropDownList { bounds:[20,132,120,152] }, « 
  78.         dlgResource +=  » okBtn:Button{bounds:[120,180,210,200] , text:’OK’ }, « 
  79.         dlgResource +=  » cancelBtn:Button{bounds:[230,180,320,200] , text:’Annuler’ } « 
  80.         dlgResource += « } »;
  81. var dlgPos = new Window(dlgResource);
  82. var SelFont = 0
  83. for ( var i = 0; i < fonts.length; i++ ) {
  84.         dlgPos.FontDDL.add( « item », fonts[i].name );
  85.         if (fonts[i].name == ‘Verdana’) {
  86.           SelFont = i;
  87.         }
  88. }
  89. dlgPos.FontDDL.items[SelFont].selected = true;
  90. for ( var i = 3; i < 20; i++ ) {
  91.         dlgPos.FontSizeDDL.add( « item », i );
  92. }
  93. dlgPos.FontSizeDDL.items[3].selected = true;
  94. //dlgPos.panel1.group0.radiobutton1.value = true;
  95. dlgPos.okBtn.onClick = function () { dlgPos.close(1); };
  96. dlgPos.cancelBtn.onClick = function () { dlgPos.close(2); };
  97. if (dlgPos.show() == 1){
  98.   var WtPosS = 4;
  99.   if (dlgPos.panel1.group0.radiobutton1.value == true){
  100.     WtPosS = 1;
  101.   }
  102.   if (dlgPos.panel1.group0.radiobutton2.value == true){
  103.     WtPosS = 2;
  104.   }
  105.   if (dlgPos.panel1.group0.radiobutton3.value == true){
  106.     WtPosS = 3;
  107.   }
  108.   if (dlgPos.panel1.group0.radiobutton4.value == true){
  109.     WtPosS = 4;
  110.   }
  111.   var CpyInfoTxt = dlgPos.copyrightInfoEt.text;
  112.   var FlattenLC = dlgPos.FlattenCb.value;
  113.   var SelFontName = dlgPos.FontDDL.selection;
  114.   var SelTxtSize = dlgPos.FontSizeDDL.selection.text;
  115.   Watermarkize(WtPosS, CpyInfoTxt, FlattenLC, SelFontName, SelTxtSize);
  116. }else{
  117.   //alert(« pas blu ! »);
  118. }
  119. function Watermarkize (WtPos, CopyTx, FlattenLy, TextFnt, TxtSize) {
  120.     var Off7 = 7;    // Distance entre le texte et le cadre
  121.     var Undent = 6;  // Distance entre le cadre et le bord de la photo
  122.     // On crée un nouveau calque texte
  123.     var infoLayer = AD.artLayers.add();
  124.     infoLayer.kind = LayerKind.TEXT;
  125.     infoLayer.name = « Exif WM – Texte »
  126.     var TI = infoLayer.textItem;
  127.     TI.font = TextFnt;
  128.     TI.size = TxtSize;
  129.     TI.color = white;
  130.     // Voici le texte, « \u000D » représente un retour chariot ( caractère n°13, D en hexadécimal)
  131.     TI.contents = cameraModel
  132.     TI.contents = TI.contents + « \u000D » + FLength +  »  «  + expTime +  »  «  + FStop +  »  «  + ISO +  » ISO »;
  133.     TI.contents = TI.contents + « \u000D » + « © « + phoTime + » « + CopyTx ;
  134.     TI.kind = TextType.PARAGRAPHTEXT;
  135.     TI.nobreak = true;
  136.     // On récupère la taille du texte
  137.     var TextWidth  = (infoLayer.bounds[2]-infoLayer.bounds[0]);
  138.     var TextHeight = (infoLayer.bounds[3]-infoLayer.bounds[1]);
  139.     var TextLeft = AD.width(TextWidth + Off7 * 2 + Undent);
  140.     var TextTop = AD.height(TextHeight + Off7 * 2 + Undent);
  141.     if (WtPos == 1) {
  142.       TextLeft = (Off7 * 2 + Undent);
  143.       TextTop  = (Off7 * 2 + Undent);
  144.     };
  145.     if (WtPos == 2) {
  146.       TextLeft = AD.width(TextWidth + Off7 * 2 + Undent);
  147.       TextTop  = (Off7 * 2 + Undent);
  148.     };
  149.     if (WtPos == 3) {
  150.       TextLeft = (Off7 * 2 + Undent);
  151.       TextTop  = AD.height(TextHeight + Off7 * 2 + Undent);
  152.     };
  153.     // Et on le déplace en bas à gauche, position calculée avec les distances mises en place plus haut
  154.     TI.position = [TextLeft,TextTop];
  155.     // On crée un nouveau calque, pour y dessiner notre cadre
  156.     var CadreLayer = AD.artLayers.add();
  157.     CadreLayer.name = « Exif WM – Cadre »;
  158.     var CadreRight = TextLeft + TextWidth + Off7;
  159.     var CadreBottom = TextTop + TextHeight + Off7;
  160.     // cadre dont la taille et la position sont calculées selon celles du texte
  161.     var selectionBounds = [[TextLeft-Off7,TextTop-Off7], [CadreRight,TextTop-Off7], [CadreRight,CadreBottom], [TextLeft-Off7,CadreBottom]];
  162.     // On crée une sélection
  163.     AD.selection.select(selectionBounds,SelectionType.REPLACE,0,false);
  164.     // On la remplis d’un aplat de noir visible à 35% ( transparent à 65% donc )
  165.     AD.selection.fill(black,ColorBlendMode.VIVIDLIGHT,35);
  166.     // Puis on trace le contour blanc visible à 100%
  167.     AD.selection.stroke(white,3,StrokeLocation.OUTSIDE, ColorBlendMode.VIVIDLIGHT, 100, false);
  168.     AD.selection.deselect();
  169.     // On place le texte au dessus du cadre ( pour qu’il ne soit pas affecté par l’aplat noir )
  170.     infoLayer.move(CadreLayer,ElementPlacement.PLACEBEFORE);
  171.     // On lie les deux calques ( ils bougeront ensemble )
  172.     CadreLayer.link(infoLayer);
  173.     if (FlattenLy) {
  174.       AD.flatten();
  175.     }
  176. }
  177. // on remet en place les unités de mesure sauvegardées
  178. preferences.rulerUnits = defaultRulerUnits;
  179. preferences.typeUnits = defaultTypeUnits;

Formatage du code : GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter)