PDF-ShellTools > Ideas/Suggestions

Script to count how many colour pages in PDF?

<< < (2/4) > >>

RTT:
How is the page ink coverage calculated?
Something like the percentage of the CMYK colors? This gives 4 values, i.e. the percentage of cyan, magenta, yellow and black. Are this 4 values what you want to calculate?

nightslayer23:
basically yes. whatever ISN'T white space..

Let's say the page was split in half.. half white space, half Purple.

I would need it to spit out a percentage of 50% and equate that to being a Medium coverage because it falls between the 34-66% threshold.

But given the fact CMYK values mix together and both have to print to say produce Purple, it would need to know not to add 50% Yellow to 50% Cyan to makeup that Purple. Which would be 50+50 = 100% which is false because it isn't a 100% file..

nightslayer23:
also, the original code that spat out a csv file was a handy part of the function.. could that come back into the second script?
the csv file also didn't appear to put the colour and black&white values as separate collumns in excel for easy sum totalling.

RTT:

--- Quote from: nightslayer23 on May 18, 2017, 03:09:10 AM ---whatever ISN'T white space..

--- End quote ---
Not easy to find a ImageMagick set of commands to calculate this properly for all the situations, even because I'm not an expert on this subject, but thresholding the image, to convert to black all non-white pixels, and then calculating the page percentage of black pixels seems to be giving good results.

--- Code: ---var imo = new ActiveXObject("ImageMagickObject.MagickImage.1");
var fso = new ActiveXObject("Scripting.FileSystemObject");

var tmpfolder = fso.GetSpecialFolder(2 /*TemporaryFolder*/ );
var InfoFilename = tmpfolder + '\\PagesInfo.txt';

var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;

for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    ProgressBar.position = i + 1;
    var file = pdfe.SelectedFiles(i);
    var FileMetadata = file.Metadata;

    //Bypass already processed files.
    if (FileMetadata.InkCoverage) {
        pdfe.echo(file.filename + ': Ink coverage = ' + FileMetadata.InkCoverage);
        pdfe.echo(' [Already set]', 0xFF, 1);
        continue;
    }

    pdfe.echo('Processing ' + file.filename + ' (' + file.NumPages + ' pages)');
    try {
        //use imagemagick to render each pdf page, convert all non-white colors to black
        //and calculate the average of black pixels, that correspond to the percentage of non-white area.

        //imo.convert(file.filename, "-fuzz","1%","-fill","white","opaque","white","-fill","black","+opaque","white","-format", "%[fx:100-mean*100]\n", "info:" + InfoFilename);               
        imo.convert(file.filename, "-colorspace", "gray", "-auto-level", "-threshold", "99%", "-format", "%[fx:100-mean*100]\n", "info:" + InfoFilename);

        //read the result info file, that contains a line of ink coverage percentage value for each page.
        var f = fso.GetFile(InfoFilename);
        var fts = f.OpenAsTextStream();
        var PagesInkCoverage = fts.ReadAll().split('\n');
        fts.Close();
        f.Delete();

        //calculate the document total ink coverage by averaging the by page values.
        var InkCoverage = 0;
        for (var index = 0, len = PagesInkCoverage.length - 1; index < len; index++) {
            InkCoverage += Number(PagesInkCoverage[index]);
        }
        InkCoverage = Math.round((InkCoverage / (len ? len : 1)));

        pdfe.echo(file.filename + ': Ink coverage=' + InkCoverage + '%', 0, 2);

        if (FileMetadata.InkCoverage !== InkCoverage.toString()) {
            FileMetadata.InkCoverage = InkCoverage;
            if (FileMetadata.CommitChanges()) {
                pdfe.echo(' [OK]', 0x006400, 1);
            } else {
                pdfe.echo(' [Setting metadata failed]', 0xFF0000, 1);
            }
        } else {
            pdfe.echo(' [Already set]', 0xFF, 1);
        }

    } catch (e) {
        pdfe.echo(file.filename + ' : ', 0, 2);
        pdfe.echo(e.name + ' ( ' + e.message + ' )', 0xff0000, 1);
    }
}

pdfe.echo('Done');

--- End code ---
This script expects a custom property named InkCoverage and to show in the Shell this ink coverage percentage value as ranges named "Line", "Medium" or "High", this custom property needs to be configured as depicted in the attached screenshots.

If it's not giving the expected results, better if you ask in a ImageMagick forum on how to calculate this and then we can update the script with a better set of image processing/analysis commands.

RTT:

--- Quote from: nightslayer23 on May 18, 2017, 03:13:01 AM ---also, the original code that spat out a csv file was a handy part of the function.. could that come back into the second script?

--- End quote ---
That's something you can try yourself. ;) You just need to copy some lines from the first script.
But probably better if you use the included "Export metadata to MS Excel or OpenOffice Calc" script for that propose. After the PDFs are processed, you can run this script anytime to fill the spreadsheet with all the defined properties of the selected PDFs.


--- Quote ---the csv file also didn't appear to put the colour and black&white values as separate collumns in excel for easy sum totalling.

--- End quote ---
The last time I checked it included these values as independent columns. If you are using the MS Excel, you need to open the csv manually. The script automatically opens the generated csv, but Excel fails to properly import the data this way. It works fine with the OpenOffice Calc, that is what I use.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Reply

Go to full version