I have a PDF that contains a gradient that I'd like to render to a bitmap with more than 8 bits of resolution.
I've come to the conclusion that I can't do this with ghostscript, even using the tiff64nc
driver, since something is internally quantizing the output to 8 bits, even though it is rendered into a 16 bit tiff file.
I saw that vips is not using ghostscript under the hood when reading a PDF and I'd like to see if I can read in the PDF and write out a 16 bit tiff.
I tried pyvips like so
import pyvips
p = pyvips.Image.pdfload('test.pdf', page=0, n=1, dpi=300)
p.tiffsave('t.tif', compression='lzw', bitdepth=16)
But obviously this doesn't work, since bitdepth is limited to 8. I know vips can write 16 bit tiffs.
What's the right way to do this? Does the underlying PDF library have the ability to rasterize a 16 bit image?
libvips can be configured to use one of poppler, pdfium or imagemagick (which in turn uses ghostscript) for PDF rendering.
Poppler and pdfium are derived from desktop PDF previewers and can't do 16 bits (almost no one needs that for a desktop preview). Ghostscript on the other hand is a pretty high end PDF renderer and can do 16 bits -- for example with this PDF (Audi R8 brochure) I can run:
And I get a true 16-bit PNG (not an 8-bit image that's been shifted up). Note that you need the
-depth
option before the PDF to make sure it's loaded in 16 bit mode. The[0]
means "render page zero". That's IM 6.9.11-60 on ubuntu 23.10.Here's the histogram of the first page:
There's some quantisation, but there are many more than 256 values.
I would be a little cautious with ghostscript. It has a relatively restrictive AGPL licence and you might need to pay, depending on how you use it.