Parsing text files

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

abruzzi

Parsing text files

Post by abruzzi »

Sorry to make this my first post. I have a large number of TIFF images I need to process ( >250k). I was able to process most of them with ImageMagick, but I still have about 30k that ImageMagick (and netpbm, and libtiff, and everything but Photoshop) doesn't see as a readable image. All I need to do is load each image, then save-as it back out. Once that's done, I can go back to ImageMagick and reprocess the files as I need to.

My first thought was to use Applescript, but with CS5.5 and 10.7 (Lion) I kept getting an error simply opening the file. I read enough to suggest that applescript support is pretty broken (I don't have the error, something like -1708, just when I was telling the application to open the file.)

So I tested JavaScript, asking it to open and save a sample file, and it worked fine. Now I now I need to ask it to open and save 30k files. I have a CSV file with all the files I would like to step through. I tried to fopen() the file, but that doesn't seem to be implemented in Photoshop's Javascript engine.

Any thoughts on what might work to load in a CSV file for parsing on Photoshop CS5.5 on a Mac?

thanks,

Geof

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Parsing text files

Post by Mike Hale »

Here is a rough outline for reading a csv file using Extendscript.

Code: Select all// make a file reference to the csv file
var myCSV - new FIle('~/destkop/data.csv');
myCSV.open('r');
var lines = new array;
while(!myCSV.eof){
    lines.push(myCSV.readln());
}
myCSV.close();
// the array lines should now be populated with each element being one line from the csv

Note there are other ways to read a CSV file but this should get you started.
abruzzi

Parsing text files

Post by abruzzi »

thanks!

(also--sorry, I noticed I put this in the wrong subforum.)

Geof
abruzzi

Parsing text files

Post by abruzzi »

I figured I post my result, in case anyone finds it useful. This script was meant to deal with a specific task, and as such, it doesn't deal with specail cases (like quoted fields in a CSV). It also doesn't do much in the way of error checking since I'll be babysitting it when it runs.

My CSV file looks like this (except much longer):

Code: Select all9122494,1991-12-30,WARRANTY DEED,REDACTION_APPS/DEEDS/0/305/312545.bin
9122495,1991-12-30,WARRANTY DEED,REDACTION_APPS/DEEDS/0/305/312546.bin
9122498,1991-12-30,C,REDACTION_APPS/DEEDS/0/305/312555.bin
9122500,1991-12-30,QUITCLAIM DEED,REDACTION_APPS/DEEDS/0/305/312562.bin
9122503,1991-12-30,C,REDACTION_APPS/DEEDS/0/305/312565.bin,REDACTION_APPS/DEEDS/0/305/312566.bin
9122504,1991-12-30,INSTRUMENT OF DISTRIBUTION,REDACTION_APPS/DEEDS/0/305/312567.bin,REDACTION_APPS/DEEDS/0/305/312568.bin
9122505,1991-12-30,QUITCLAIM DEED,REDACTION_APPS/DEEDS/0/305/312569.bin


Column 1 is the instrument number, which is used to name the new file. Columns 2 and 3 (date and document type) are ignored. Columns 4 + are pages of the document. Many only have 1 page, some have many pages, up to 300 pages. Ultimately I need to turn these into multipage TIFFs, one doc per instrument.

I already have a script (php cli) that uses ImageMagick to combine single page tiffs into multipage tiffs for all files in a directory, when they are naled like this: IIIIII-P.tif (where IIIIII is the instrument number and P is the page number. So all I have to do is convert the abitrarily named files into IIIIII-P.tif name. Also, like I said above, loading and saving from photoshop converts the file to a more standard TIFF that ImageMagick can read.

(You'll notice that the files are named .bin--this they are actually TIFFs but the System that stores them, ApplicationXtender, names them .bin.)

Finally, since these were all scanned from microfilm, I figured I'd take the time to invert the image back to black-on-white, rather than the negative format they were saved in. So here is my code:

Code: Select all
TiffSaveOptions = new TiffSaveOptions();

var csv = "/Users/abruzzi/Desktop/bad_deeds/MissingObjects-1990-1993.csv"
var originalDIR = "/Volumes/images/AX_Images/";
var saveDIR = "/Users/abruzzi/Desktop/90s/";

// make a file reference to the csv file
var myCSV = new File(csv);
myCSV.open('r');


while(!myCSV.eof){
   var line = myCSV.readln();
   
   var lineItems = new Array;
   lineItems = line.split(",");
   
   for (i=3;i<lineItems.length;i++) {
   
   
      var fileRef = File( originalDIR + lineItems);
      var docRef = app.open(fileRef, OpenDocumentType.TIFF);
      
      var layerRef = docRef.artLayers.getByName("Background");
      layerRef.invert();
      
      var saveFile = saveDIR + lineItems[0] + "-" + (i-2) + ".tif";
            
      tifFile = new File( saveFile );
      docRef.saveAs(tifFile, TiffSaveOptions);

      docRef.close(SaveOptions.DONOTSAVECHANGES);
      
   }
   
}

myCSV.close();