Okay, this is driving me crazy. This script works fine on CS5 but not on CS3. The person who reported this to me is running CS3 on Windows XP. I have CS5 on Windows 7-64, and reinstalled CS3 to try to figure this out. Nothing obvious in the code is jumping out at me. I thought the XMP might be causing problems for CS3, but the XMP function is Xbytors, which I've seen posted as working on CS3 just fine. To see if permissions might be a problem, I tried running the script out of the default folders for each version, a neutral folder with no permissions and running CS3 as an administrator. In all cases CS3 would create the CSV file with a header and process the images, but no data was written to the CSV. CS5 would write the data just fine.
Code: Select all
//----------------------------- Histogram ---------------------------
// Histogram functions by Xbytor
Histogram = function Histogram() {}
Histogram.range = function(hist, cutoff) {
var HIST_MAX = hist.length;
cutoff = cutoff || 0;
if (Levels.dumpHistogram) { // change this to false to see what
// the histogram numbers really are
// debug stuff...
var str = '';
for (var i = 0; i < HIST_MAX; i++) {
str += hist + ",";
}
confirm(cutoff + '\r' + str);
}
// find the minimum level
var min = 0;
while (min < HIST_MAX && hist[min] <= cutoff) {
min++;
}
// find the maximum level
var max = HIST_MAX-1;
while (max >= 0 && hist[max] <= cutoff) {
max--;
}
var r = {};
r.min = min;
r.max = max;
if (false) {
alert(r.min + ':' + r.max);
}
// we need at least 2 distinct levels
if (r.min != HIST_MAX && r.max != -1 && r.max != r.min) {
// we also need to be sure that we're not already auto-leveled
if (!(r.min == 0 && r.max == (HIST_MAX-1))) {
return r;
}
}
return null;
}
Histogram.mean = function(hist) {
var acc = 0;
var cnt = 0;
for (var i = 0; i < hist.length; i++) {
acc += i*hist;
cnt += hist;
}
return acc/cnt;
}
Histogram.median = function(hist) {
var cnt = 0;
for (var i = 0; i < hist.length; i++) {
cnt += hist;
}
cnt = cnt/2;
var acc = 0;
for (var i = 0; i < hist.length; i++) {
acc += hist;
if (acc > cnt) return i-1;
}
return -1;
}
function curveAdjsLayer(input,output) {
cTID = function(s) { return app.charIDToTypeID(s); };
var desc = new ActionDescriptor();
var classRef = new ActionReference();
classRef.putClass( cTID('AdjL') );
desc.putReference( cTID('null'), classRef );
var adjsDesc = new ActionDescriptor();
var curveDesc = new ActionDescriptor();
var adjsList = new ActionList();
var curveAdesc = new ActionDescriptor();
var tragetRef = new ActionReference();
tragetRef.putEnumerated( cTID('Chnl'), cTID('Chnl'), cTID('Cmps') );
curveAdesc.putReference( cTID('Chnl'), tragetRef );
var pointsList = new ActionList();
var blackPtDesc = new ActionDescriptor();
blackPtDesc.putDouble( cTID('Hrzn'), 0.000000 );
blackPtDesc.putDouble( cTID('Vrtc'), 0.000000 );
pointsList.putObject( cTID('Pnt '), blackPtDesc );
var midPtDesc = new ActionDescriptor();
midPtDesc.putDouble( cTID('Hrzn'), input );
midPtDesc.putDouble( cTID('Vrtc'), output );
pointsList.putObject( cTID('Pnt '), midPtDesc );
var whitePtDesc = new ActionDescriptor();
whitePtDesc.putDouble( cTID('Hrzn'), 255.000000 );
whitePtDesc.putDouble( cTID('Vrtc'), 255.000000 );
pointsList.putObject( cTID('Pnt '), whitePtDesc );
curveAdesc.putList( cTID('Crv '), pointsList );
adjsList.putObject( cTID('CrvA'), curveAdesc );
curveDesc.putList( cTID('Adjs'), adjsList );
adjsDesc.putObject( cTID('Type'), cTID('Crvs'), curveDesc );
desc.putObject( cTID('Usng'), cTID('AdjL'), adjsDesc );
executeAction( cTID('Mk '), desc, DialogModes.NO );
};
var x = 600;//x,y of sample area in pixels
var y = 1390;
var size = 150;// size of sample in pixels ie 10x10px
var workFolder = Folder.selectDialog('Select the folder to process');
var files = workFolder.getFiles(/jpg$/i);// get an array of jpg files to process
files.sort();
var csvFile = new File(workFolder+"/"+"JPEG_histogram.csv");
if( !csvFile.exists ){// no csv file so create one and write header
csvFile.open('w');
csvFile.writeln( 'FileName,Histogram,Temperature,Tint,Date,Time');// just a guess at what you want for a header
csvFile.close();
}
//open files
for(var f = 0; f<files.length;f++){
app.open(files[f]);
var myDocument = app.activeDocument
app.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length-1];
//select sample area
activeDocument.selection.select([[x,y],[x+size,y],[x+size,y+size],[x,y+size]]);
//get the histogram of sample
var area = activeDocument.histogram;
var xmp = activeDocument.xmpMetadata.rawData;
//find white balance temperature
var Temperature = parseMetadata(xmp,"crs:Temperature");
//find white balance tint
var Tint = parseMetadata(xmp,"crs:Tint");
//get date and time picture was created
var CreateDate = parseMetadata(xmp,"exif:DateTimeOriginal");
//get date and time picture was modified
var ModDate = parseMetadata(xmp,"xmp:ModifyDate");
function parseMetadata(xmp, tag) {
var re = new RegExp('<' + tag + '>(.+)</' + tag + '>');
var m = xmp.match(re);
if (!m) {
re = new RegExp("<[^:]+:" + tag + ">(.+)</[^:]+:" + tag + '>');
m = this.xmp.match(re);
}
return (m ? m[1] : '');
};
//Split image created date and time
var chunks = CreateDate.split("T");
var date = chunks[0]; // 2007-12-01
var time = chunks[1]; // 09:57:41
// break apart date
chunks = date.split("-");
var yyyy = chunks[0];
var mm = chunks[1];
var dd = chunks[2];
// break apart time
chunks = time.split(":");
var hr = chunks[0];
var mn = chunks[1];
if(chunks[2] == undefined) chunks[2] = "00";
var ss = chunks[2];
// EXIF create date
var date =mm +"-"+ dd +"-"+ yyyy;
var time = hr +":"+ mn +":"+ ss;
//CSV file
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
csvFile = File(thePath+"/"+"JPEG_histogram.csv");
csvFile.open("a");
csvFile.write(theName+","+Histogram.median(area)+ "," + Temperature + "," + Tint +","+ date +","+ time);
csvFile.write("\r");
csvFile.close();
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
Any help is greatly appreciated.
Thanks,
Chris
PS Happy Thanksgiving to my American friends!
Script works on CS5 but not CS3
Script works on CS5 but not CS3
One thing you could try is to change:
Code: Select allcsvFile.open("a");
To.
Code: Select allcsvFile.open('e');
csvFile.seek(0,2);
This line is pointless as you are working with jpg's
Code: Select allapp.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length-1];
Oh and your sort isn't doing anything.
Code: Select allcsvFile.open("a");
To.
Code: Select allcsvFile.open('e');
csvFile.seek(0,2);
This line is pointless as you are working with jpg's
Code: Select allapp.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length-1];
Oh and your sort isn't doing anything.
Script works on CS5 but not CS3
Paul MR wrote:One thing you could try is to change:
Code: Select allcsvFile.open("a");
To.
Code: Select allcsvFile.open('e');
csvFile.seek(0,2);
Paul, thank you! This took care of it right away. Any idea why appending doesn't work in CS3? I can't find anything in the Adobe docs.
This line is pointless as you are working with jpg's
Code: Select allapp.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length-1];
You're right. I changed this from a script for PSDs and missed this.
Oh and your sort isn't doing anything.
How do you know this? Is it from experience or can you see it debugging? I've been trying to get a handle on debugging with ESTK, but a lot of the information it shows is as clear as mud to me. The data browser looks like everything plus the kitchen sink is thrown in there. Since JavaScript isn't compiled, is it impossible to see what is going on line-by-line?
Thanks again,
Chris
Code: Select allcsvFile.open("a");
To.
Code: Select allcsvFile.open('e');
csvFile.seek(0,2);
Paul, thank you! This took care of it right away. Any idea why appending doesn't work in CS3? I can't find anything in the Adobe docs.
This line is pointless as you are working with jpg's
Code: Select allapp.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length-1];
You're right. I changed this from a script for PSDs and missed this.
Oh and your sort isn't doing anything.
How do you know this? Is it from experience or can you see it debugging? I've been trying to get a handle on debugging with ESTK, but a lot of the information it shows is as clear as mud to me. The data browser looks like everything plus the kitchen sink is thrown in there. Since JavaScript isn't compiled, is it impossible to see what is going on line-by-line?
Thanks again,
Chris
Script works on CS5 but not CS3
craezer wrote:Any idea why appending doesn't work in CS3? I can't find anything in the Adobe docs.
'a' for append was added in CS4. If you look in the CS3 tools guide(page 47) the only modes listed for open are 'r', 'w',and 'e'.
'a' for append was added in CS4. If you look in the CS3 tools guide(page 47) the only modes listed for open are 'r', 'w',and 'e'.