Skip to main content
johnandersonpixel
Known Participant
November 21, 2016
Question

Saving Multiple versions.

  • November 21, 2016
  • 2 replies
  • 2246 views

Hi,

     I am having an image in my local drive but i need that image into 5000 different separate images with the names given in excel data. or csv file.

Basically the  single main image should open in PS and save as a copy in .jpg format with the file name mention in .csv file one by one. So i need to generate 5k files in different name mention in excel data using the same image.

Is that possible to get the data from csv file ??

This topic has been closed for replies.

2 replies

johnandersonpixel
Known Participant
November 28, 2016

Windows

Stephen Marsh
Community Expert
Community Expert
November 28, 2016

A quick Google search:

windows script to duplicate file n times and sequentially rename

Turned this up:

Windows batch file to make multiple copies of a single file, with each copy being assigned a unique filename - Stack Ove…

johnandersonpixel
Known Participant
November 28, 2016

Yeah i tried this method previously but i cant able to do the renaming as per excel data thats the issue in this and also every time we need re-edit the path and no of looping time in below.

JavierAroche
Inspiring
November 21, 2016

Hey johnandersonpixel​, yes you can achieve this with ExtendScript. I would however, recommend doing this through Node or Terminal since it would be faster. The only benefit I see about doing it through Photoshop is that you can control the quality of the export. You can easily save as any file type, save for web, etc...

There are different ways of doing this. Here's how I would do it through ExtendScript / Photoshop. I added comments so it was easier to read.

// Define paths

var imgFile = new File('/Users/javier/Desktop/Delete/2016_11_21/test/a.png')

var csvFile = new File('/Users/javier/Desktop/csvFile.csv');

// Read file

csvFile.open() // Open File

content = csvFile.read(); //Store contents in variable

content = content.split('\n'); // Split files by end of line

csvFile.close(); // Close File -- Always Close Files!

// Open image

var doc = app.open(imgFile);

// Define JPG export settings

var JPGSettings = new JPEGSaveOptions();

    JPGSettings.quality = 12;

// Loop through names in CSV

for(var i = 0; i < content.length; i++) {

    // Define new file path

    var newFileName = new File(doc.path + '/' + content + '.jpg');

   

    // Save file as JPG

    doc.saveAs(newFileName, JPGSettings, true, Extension.LOWERCASE);

}

// Close the file

doc.close();

This is the CSV file I generated for this example

Hope it helps!

johnandersonpixel
Known Participant
November 21, 2016

Thanks a lot Javier is there any other option to save this more faster

JavierAroche
Inspiring
November 21, 2016

johnandersonpixel​ - I'm running MacOS, so I would use Sips. Sips lets you convert an image to different file formats from the Terminal.

In this example, I converted a PNG to a JPEG.

sips -s format jpeg "/Users/javier/Desktop/Delete/2016_11_21/test/a.png" --out "/Users/javier/Desktop/Delete/2016_11_21/test/image_01.jpg"

This obviously works for 1 image only. But you can create a script that iterates through the amount of files that you need.

That's why I mentioned Node. With Node you could create a simple JavaScript file to read your CSV file, using fs.readFileSync​, loop through the list of names (same logic as the script I listed earlier), and execute the Sip command using Node's child_process.exec​ for each file that you need.

If you're on Windows, then you could use something like ImageMagick instead of Sips.

If you're not familiar with Node, then you could do it the "dirty" way through ExtendScript without even using Photoshop...

// Define paths

var imgFile = '/Users/javier/Desktop/Delete/2016_11_21/test/a.png';

var outputPath = '/Users/javier/Desktop/Delete/2016_11_21/test';

var csvFile = new File('/Users/javier/Desktop/csvFile.csv');

// Read file

csvFile.open() // Open File

content = csvFile.read(); //Store contents in variable

content = content.split('\n'); // Split files by end of line

csvFile.close(); // Close File -- Always Close Files!

// Loop through names in CSV

for(var i = 0; i < content.length; i++) {

    // Define new file path

    var newFile = outputPath + '/' + content + '.jpg';

   

    // Save file as JPG

    app.system('sips -s format jpeg \"' + imgFile + '\" --out \"' + newFile + '\"');

}