Author Topic: Script to count how many colour pages in PDF?  (Read 18990 times)

0 Members and 1 Guest are viewing this topic.

nightslayer23

  • Newbie
  • *
  • Posts: 95
Re: Script to count how many colour pages in PDF?
« Reply #15 on: July 19, 2017, 08:16:14 AM »
Is there a way for the file to be flattened first before doing this conversion? Some work perfectly, but others come out at a really high percentage when they aren't technically going to print that way. I figured it was looking at other layers or some other hidden info and converting that to bw too.
Are these trouble PDF layers set to be visible in the PDF reader (screen mode) and hidden when printed? If that's the case, edit the file delegates.xml, where you have ImageMagick installed, and change the line "<delegate decode="ps:alpha" stealth="True" command="&quot;@PSDelegate@&quot; -q -dQUIET -..." to include the -dPrinted parameter.

Quote
So would i be possible in code to first flatten layers before running the check?
When the ImageMagick tool calls the Ghostscript to convert each of the PDF pages to an image, that then uses to run the color check, is effectively flattening the PDF. If the issue is not the mentioned above (these layers are set to be hidden only when the PDF is printed) and even hidden layers are being rendered too, then that's an issue with Ghostscript.
If you have Acrobat, I suppose the script can automate it to flatten the PDF layers to a temporary PDF file and then run the check on that PDF.

Quote
I actually batch flattened layers in acrobat and it didn't solve the issue.. I had to batch flatten AND convert everything to CMYK to get it to work.
Can't opine without a sample file.

Quote
In the optimizer tool, can CMYK and RGB colour spaces be added somehow?
I'm not understanding your question. Please explain this better.

They view hidden as well. but I'm assuming that's why they come out saying an extremely high percentage.. because once I render the file, goes from saying 75% & 99% to 12% and 22% for example - which is how they view as well.

RTT

  • Administrator
  • *****
  • Posts: 909
Re: Script to count how many colour pages in PDF?
« Reply #16 on: July 20, 2017, 12:01:52 AM »
Did you tested with the -dPrinted parameter I suggested in my previous reply?

You can easily verify if indeed the hidden layers are being erroneously rendered. From a command prompt, run (change the file paths to your system specifics):
"C:\Program Files (x86)\ImageMagick-7.0.5-Q16\magick.exe" convert "Path\To\The\PDF.pdf" "Path\Where\To\Put\The\Page\Renders\Image.png"

You can also check what image is being generated in the script, to quantify the ink utilization:
"C:\Program Files (x86)\ImageMagick-7.0.5-Q16\magick.exe" convert "Path\To\The\PDF.pdf" -colorspace gray -auto-level -threshold 99% "Path\Where\To\Put\The\Page\Renders\Image.png"

Nick Riviera

  • Newbie
  • *
  • Posts: 22
Re: Script to count how many colour pages in PDF?
« Reply #17 on: August 18, 2022, 07:53:07 AM »
hello,
I also tried this script but  is not working or did I do something wrong. It gives me this "Error (11,0): Automation server can't create object"
I installed this version "ImageMagick-7.1.0-46-Q16-x86-dll" because there is no other older version and this version of https://www.ghostscript.com/releases/gsdnld.html
Thank you

That's functionality not directly available from the scripts API but we can create a script to automate the ImageMagick tool and get that info.
The idea is to render each PDF page and analyze the result bitmaps for color content.

Made some test and here is a sample script that creates a .csv file with a "Color Pages Count" and "BW/Gray Page Count" columns. It renders each PDF page, converts the result bitmaps to the HSI colorspace and computes the mean value of the saturation channel. The page is considered colorized if this value is higher than 0, or BW/Gray otherwise. You may adjust this threshold to your needs.


To test it, just import the attached .myscript file into the PDF-ShellTools My Scripts, and you will get a "Number of Color and BW/Gray pages" named script, you can invoke for all the selected PDF files from the Windows shell PDF files context menu, from the PDF-ShellTools>My Scripts sub menu.
The scrip needs to have the 32-bit version of the ImageMagick tool installed. I've tested with the ImageMagick-7.0.5-5-Q16-x86-dll.exe one. While installing, make sure you select the "Install ImageMagickObject OLE Control for VBScript,..." option, under the "additional tasks" page of the installer.
The ImageMagick also needs to have the Ghostscript tool installed, to handle the PDF format.

If the script is performing as needed we can change it to put the info into custom metadata properties, as you suggested.

RTT

  • Administrator
  • *****
  • Posts: 909
Re: Script to count how many colour pages in PDF?
« Reply #18 on: August 19, 2022, 02:47:44 AM »
It seems the ActiveX ImageMagickObject is not included anymore in the installer (Windows Installer option ImageMagickObject is missing).
Get the old version installer, that includes it, from the Wayback Machine website. Don't forget to select the "Install ImageMagickObject OLE Control for VBScript,..." when installing it.

Nick Riviera

  • Newbie
  • *
  • Posts: 22
Re: Script to count how many colour pages in PDF?
« Reply #19 on: August 22, 2022, 08:36:52 AM »
Thank you.

works with this version ImageMagick-7.1.0-8-Q16-HDRI-x86-dll.exe


It seems the ActiveX ImageMagickObject is not included anymore in the installer (Windows Installer option ImageMagickObject is missing).
Get the old version installer, that includes it, from the Wayback Machine website. Don't forget to select the "Install ImageMagickObject OLE Control for VBScript,..." when installing it.