1

Using pdfimages -all on a .pdf file, each page of which is text, I'm getting 3 images for each page in the pdf:

Foo-001-000.jp2
Foo-001-002.png
Foo-001-002.jb2e

The first file is mostly blank, but contains some ghostly background plus an occasional piece of text. The second file is black and white and appears to be some kind of mask, perhaps identifying where the text in the third file is located (?) The third file I am not able to view in Ubuntu's image viewer or gimp.

If I use -png I similarly get three images, but all are .png's. Most (almost all) of the pdf's text in in the third image.

pdfimages -list looks like this:

page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image     829  1254  rgb     3   8  jpx    yes     3659  0   150   150 76.2K 2.5%
   1     1 image     829  1254  rgb     3   8  image  yes     3663  0   150   150 5250B 0.2%
   1     2 mask     1658  2508  -       1   1  image  yes     3663  0   300   300 5250B 1.0%
   2     3 image     934  1254  rgb     3   8  jpx    yes       11  0   150   150 85.6K 2.5%
   2     4 image     934  1254  rgb     3   8  image  yes       15  0   150   150 14.1K 0.4%
   2     5 mask     1868  2508  -       1   1  image  yes       15  0   300   300 14.1K 2.5%
   3     6 image     858  1243  rgb     3   8  jpx    yes       47  0   150   150 78.0K 2.5%
   3     7 image     858  1243  rgb     3   8  image  yes       51  0   150   150 7681B 0.2%

Could someone help me understand what I've got here, and how I might combine these three images to get a single images for each page. Or equivalently, just extract single images per page. They key issue for me is to keep as much information as is available in these images. I want to avoid degradation in quality.

First File Second File Third File

18
  • What's your goal? I think pdfimages is for extracting images already in the document. If your goal is to convert the pages of the pdf to images preserving the look of the pdf, I'd use something like pdftoppm, pdftocairo or mutool instead.
    – frabjous
    Oct 22, 2022 at 15:04
  • @frabjous - The goal is to get images that look like what I see in the pdf, while preserving as much digital information as possible. The images are scans, and not very high quality as it it. I don't want to degrade that. My use of pdfimages was an attempt to preserve as much as possible. (Subsidiary goal is to understand what I'm looking at with these 3 images per page. What? Why?)
    – Diagon
    Oct 22, 2022 at 15:17
  • For those of us who cannot see the original PDF, that's still a difficult description of your goal to understand. Are the images you're hoping to extract just parts of each page, so there can be more than one? Or are you trying to get output images that look exactly like the full pdf pages, without losing any quality? If the latter, I'd still use one of the other tools I mentioned; they let you set the output resolution and compression level. Just make sure you choose something no worse than what you already have and you shouldn't lose quality.
    – frabjous
    Oct 22, 2022 at 18:11
  • 1
    All three support png output.
    – frabjous
    Oct 22, 2022 at 22:50
  • 1
    Some similarities to this question, what you want to achieve is sometimes referred to as "flatten" a PDF and rasterize without interpolation. The answers in this question all seem to use interpolation. We need a program which determines the highest resolution any of the embedded images has, use that as the document's resolution. Re-scale the lower resolution images by a power of two. Turn all offsets into integers. Then rendering can happen accurately down to the pixel.
    – Hermann
    Oct 23, 2022 at 10:23

1 Answer 1

1

I guess, you think you have a single image as a page and are surprised it's actually composited. This is a widespread method for archiving magazines as these are graphically more complex than a simple book with some images. It preserves the quality and gives a very small pdf file in the end - but unusably slow to render.

Now for the solution. You don't actually want to extract anything from the pdf. You want to render it the same way your pdf reader does. I would suggest using Ghostscript. Something like this will work:

gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r600 -dGraphicsAlphaBits=4 -sOutputFile=./img/img-%03d.png "$pdffilename"

Adjust as needed.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .