Guest User

Image operations with Photoshop and ExtendScript

a guest
Jan 31st, 2011
3,666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #target photoshop
  2.  
  3. app.bringToFront();
  4.  
  5. var Constants = {
  6.     Author: "Antelle",
  7.     OutputImageQuality: 11,
  8.     FileMask: "*.*g",
  9.     InputDir: "d:\\Photos4Habr",
  10.     OutputDir: "d:\\Photos4Habr\\Output",
  11.     AddLogo: {
  12.         LogoPath: "d:\\Logo.psd"
  13.     },
  14.     Resize: {
  15.         MaxWidth: 400,
  16.         MaxHeight: 600
  17.     }
  18. };
  19.  
  20. ProcessDir(Constants.InputDir, Constants.OutputDir);
  21.  
  22. function ProcessDir(dir, outDir) {
  23.     var folder = Folder(dir);
  24.     var files = folder.getFiles(Constants.FileMask);
  25.     var outFolder = Folder(outDir);
  26.     if (!outFolder.exists) {
  27.         if (!outFolder.create()) {
  28.             alert("Cannot create output folder");
  29.             return;
  30.         }
  31.     }
  32.     var totalFiles = 0;
  33.     for (var fileNum in files) {
  34.          var outFile = GetOutputFileName(files[fileNum], outFolder.fullName);
  35.          AddFrameToFile(files[fileNum], outFile);
  36.          totalFiles++;
  37.     }
  38.     alert(totalFiles + " files processed");
  39. }
  40.  
  41. function GetOutputFileName(file, outDir) {
  42.     return outDir + /(\/[^\/]+$)/.exec(file)[1];
  43. }
  44.  
  45. function SaveFile(outputFile) {
  46.     var isPng = /png$/i.test(outputFile);
  47.     var saveOptions;
  48.     if (isPng) {
  49.         saveOptions = new PNGSaveOptions();
  50.         saveOptions.interlaced = false;
  51.     } else {
  52.         saveOptions = new JPEGSaveOptions();
  53.         saveOptions.embedColorProfile = true;
  54.         saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  55.         saveOptions.matte = MatteType.NONE
  56.         saveOptions.quality = Constants.OutputImageQuality;
  57.     }
  58.     app.activeDocument.saveAs(File(outputFile), saveOptions, true, Extension.LOWERCASE)
  59.    
  60.     app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
  61. }
  62.  
  63. function AddLogoToFile(file, outputFile) {
  64.     var photoFile = File(file);
  65.     var logoFile = File(Constants.AddLogo.LogoPath);
  66.  
  67.     app.open(logoFile);
  68.     app.activeDocument.artLayers["Text"].copy();
  69.    
  70.     var logoWidth = app.activeDocument.width.as("px");
  71.     var logoHeight = app.activeDocument.height.as("px");
  72.     app.activeDocument.close();
  73.  
  74.     app.open(photoFile);
  75.  
  76.     var width = app.activeDocument.width.as("px");
  77.     var height = app.activeDocument.height.as("px");
  78.  
  79.     var logoLayer = app.activeDocument.artLayers.add();
  80.     logoLayer.name = "Logo";
  81.    
  82.     app.activeDocumetn
  83.  
  84.     app.activeDocument.paste();
  85.  
  86.     var shape = [
  87.         [(width - logoWidth) / 2, (height - logoHeight) / 2],
  88.         [(width - logoWidth) / 2, (height + logoHeight) / 2],
  89.         [(width + logoWidth) / 2, (height + logoHeight) / 2],
  90.         [(width + logoWidth) / 2, (height - logoHeight) / 2]
  91.     ];
  92.     app.activeDocument.selection.select(shape);
  93.  
  94.     app.activeDocument.selection.translate(
  95.         new UnitValue((width - logoWidth)/ 2, "px"),
  96.         new UnitValue((height - logoHeight) / 2, "px"));
  97.  
  98.     var minImageDimension = Math.min(width, height);
  99.     var logoScaleMultiplier = minImageDimension / 5 / logoWidth * 100;
  100.     app.activeDocument.selection.resize(logoScaleMultiplier, logoScaleMultiplier, AnchorPosition.BOTTOMRIGHT);
  101.  
  102.     app.activeDocument.selection.deselect();
  103.  
  104.     app.activeDocument.artLayers["Logo"].opacity = 75;
  105.     app.activeDocument.artLayers["Logo"].blendMode = BlendMode.LUMINOSITY;
  106.  
  107.     SaveFile(outputFile);
  108. }
  109.  
  110. function AddFrameToFile(file, outputFile) {
  111.     var photoFile = File(file);
  112.  
  113.     app.open(photoFile);
  114.  
  115.     var width = app.activeDocument.width.as("px");
  116.     var height = app.activeDocument.height.as("px");
  117.  
  118.     var backgroungLayer = app.activeDocument.artLayers[0];
  119.     backgroungLayer.isBackgroundLayer = false;
  120.  
  121.     var frameLayer = app.activeDocument.artLayers.add();
  122.     frameLayer.name = "Frame";
  123.    
  124.     frameLayer.move(backgroungLayer, ElementPlacement.PLACEAFTER);
  125.    
  126.     app.activeDocument.resizeCanvas(new UnitValue(width + 10, "px"), new UnitValue(height + 10, "px"), AnchorPosition.MIDDLECENTER);
  127.    
  128.     var color = new SolidColor();
  129.     color.rgb.red = 0xff;
  130.     color.rgb.green = 0xff;
  131.     color.rgb.blue = 0xff;
  132.     app.activeDocument.selection.fill(color);
  133.     var shape = [
  134.         [3, 3],
  135.         [3, height + 7],
  136.         [width + 7, height + 7],
  137.         [width + 7, 3]
  138.     ];
  139.     app.activeDocument.selection.select(shape, SelectionType.REPLACE, 1, false);
  140.     color = new SolidColor();
  141.     color.rgb.red = 0xa9;
  142.     color.rgb.green = 0xcc;
  143.     color.rgb.blue = 0xf5;
  144.     app.activeDocument.selection.fill(color);
  145.    
  146.     SaveFile(outputFile);
  147. }
  148.  
  149. function ResizeFile(file, outputFile) {
  150.     var photoFile = File(file);
  151.  
  152.     app.open(photoFile);
  153.  
  154.     var width = app.activeDocument.width.as("px");
  155.     var height = app.activeDocument.height.as("px");
  156.    
  157.     var widthScaleFactor = width / Constants.Resize.MaxWidth;
  158.     var heightScaleFactor = height / Constants.Resize.MaxHeight;
  159.     var maxScaleFactor = Math.max(widthScaleFactor, heightScaleFactor);
  160.    
  161.     if (maxScaleFactor > 1) {
  162.         app.activeDocument.resizeImage(
  163.             new UnitValue(width / maxScaleFactor, "px"),
  164.             new UnitValue(height / maxScaleFactor, "px"));
  165.     }
  166.  
  167.     SaveFile(outputFile);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment