Adobe Photoshop - Scripts for Image Type Conversions

From NoskeWiki
Jump to: navigation, search

About

NOTE: This page is a daughter page of: Adobe Photoshop


Here are a couple of useful Adobe Photoshop scripts for converting file formats.


Photoshop Script: Save a folder of images as JPEG

save_image_to_jpg.jsx:

// Converts a folder full of images to JPEG
// images in the same folder.
#target photoshop
 
// Open folder selection dialog (for user to chose folder):
alert("You will be prompted for the folder containing your image.\n" +
      "You should probably make a backup of this folder before proceeding.");
 
var folder = Folder.selectDialog();
if (!folder) {alert("Cancelled"); exit;}
 
// Saves currently open image to jpeg.
function saveJpeg(saveFile, jpegQuality){
  var doc = activeDocument;
  if (doc.bitsPerChannel != BitsPerChannelType.EIGHT)
    doc.bitsPerChannel = BitsPerChannelType.EIGHT;
  jpgSaveOptions = new JPEGSaveOptions();
  jpgSaveOptions.embedColorProfile = true;
  jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  jpgSaveOptions.matte = MatteType.NONE;
  jpgSaveOptions.quality = jpegQuality;
  activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
}     
 
// For each image: open, then save as JPG:
var files = folder.getFiles(/\.(jpg|jpeg|tif|tiff|bmp|png|eps)$/i);
for (var i = 0; i < files.length; i++) {
  var img = app.open(File(files[i]));
  var saveFilePath = File(files[i] + ".jpg");
  saveJpeg(saveFilePath, 8);
  img.close(SaveOptions.DONOTSAVECHANGES);
};


Photoshop Script: Export a folder of image as PNG

export_folder_to_png.jsx:

// Exports a folder full of images to PNG
// images in the same folder.
#target photoshop
 
// Open folder selection dialog (for user to chose folder):
alert("You will be prompted for the folder containing your images.\n" +
      "Files will be export with a '.png' on the end in the same folder.");
 
var folder = Folder.selectDialog();
if (!folder) {exit;}
 
// Export currently open image as PNG.
function exportPng(img, savePath){
  var opts = new ExportOptionsSaveForWeb();
  if (img.bitsPerChannel != BitsPerChannelType.EIGHT)
    img.bitsPerChannel = BitsPerChannelType.EIGHT;
  opts.PNG8 = false;
  opts.transparency = true;
  opts.interlaced = false;
  opts.quality = 100;
  opts.includeProfile = false;
  opts.format = SaveDocumentType.PNG;
  img.exportDocument(savePath, ExportType.SAVEFORWEB, opts);
}
 
// For each image: open, then save as JPG:
var files = folder.getFiles(/\.(jpg|jpeg|tif|tiff|bmp|png|eps)$/i);
for (var i = 0; i < files.length; i++) {
  var img = app.open(File(files[i]));
  var saveFilePath = File(files[i] + ".png");
  exportPng(img, saveFilePath);
  img.close(SaveOptions.DONOTSAVECHANGES);
};


Links

  • none