2

Here is the information of images in a single-page pdf file:

$ pdfimages -list p1.pdf 
page   num  type   width height color comp bpc  enc interp  object ID
---------------------------------------------------------------------
   1     0 image     900  1100  rgb     3   8  image  yes        4  0
   1     1 mask      900  1100  -       1   1  image  no         4  0    

I extract images from the pdf using pdfimages:

$ pdfimages -p p1.pdf ./p1
$ ls
p1-001-000.ppm  p1-001-001.pbm   p1.pdf 

The output p1-001-000.ppm and p1-001-001.pbm correspond to "image" and "mask" shown earlier resp.

Then I try to recover the original pdf from the "image" and "mask", using convert and pdftk in two different ways ("stamp" and "background"):

$ for i in *.{ppm,pbm}; do convert $i $i.pdf; done
$ ls
p1-001-000.ppm  p1-001-000.ppm.pdf  p1-001-001.pbm  p1-001-001.pbm.pdf  p1.pdf
$ pdftk p1-001-000.ppm.pdf stamp p1-001-001.pbm.pdf output p1stamp.pdf
$ pdftk p1-001-000.ppm.pdf background p1-001-001.pbm.pdf output p1bkg.pdf
  1. p1stamp.pdf is blank (very different from original p1.pdf), while p1bkg.pdf and original p1.pdf look similar but still different around the right and low borders.

    The output of pdfimages shown at beginning has a column named "type"

    type

    The image type. Possible values are: image (an opaque image), mask (a monochrome image mask), smask (a soft-mask image) and stencil (a monochrome mask image used for painging a color or a pattern).

    Note: Tranparency in PDF for images is created by using two separate PDF objects: one for the image and one for the mask or smask. The mask/smask belonging to a transparent image always directly follows image in the listing.

    Are "mask", "smask" and "stencile" used as foreground or background in a pdf file?

    How are they different from "watermark"? Is "watermark" always used as background?

  2. Another issue is that the two new pdf files p1stamp.pdf and p1bkg.pdf are both 476 KB, much larger than the original p1.pdf which is only 94 KB. Even if I compress the new pdf files by following command, their sizes don't change:

    $ pdftk p1bkg.pdf output p1bkgcomp.pdf compress
    

How can we realistically do the opposite of the splitting work by pdfimages, i.e. recover original pdf file from their images extracted by pdfimages. so that the appearance and size are the same or close to the original pdf file?

Thanks.

1 Answer 1

2

As the second image is a mask it needs to be applied like a mask - this can be done like this:

convert ./p1-001-000.ppm  ./p1-001-001.pbm -alpha Off -compose CopyOpacity -composite output.pdf

source

This results in a PDF which has the bottom black edge removed/hidden by the mask, but is much larger in file size (485.3Kb vs 96Kb) and has a larger PDF page.:

File properties of each file screenshot

Bear in mind that PDFs are more than just images layered on top of each over - they have text, formatting, etc. So by only extracting the images from the pdf file, information is lost. At a guess in this case looking at the original PDF file in LibreOffice Draw, the edges of the image are over the edge of the PDF page, so are not shown (I think the images in each are also scaled to different sizes... - image extracted as PNG from LibreOffice Draw here for comparison)

5
  • Thanks. (1) in the link to imagick, after the first example, "black in a mask is transparent, while white is opaque, so all we need to do is draw black over anything we don't want visible. " But in the fourth/last image, we can only see the part of the original image corresponding to the mask's white portion. So is the quote contradictory to the example? (2) how shall we use pdftk correctly to add the mask pdf to the image pdf, so that the output pdf shows the image pdf instead of mask? Shall I make the mask image file transparent first before convert it to a mask pdf file?
    – Tim
    Commented Feb 17, 2015 at 18:50
  • (3) the result of using convert is still quite different from the original on the right and lower borders. Maybe pdftk can do better, if we know how to use it correctly? (pdftk is said to be able to stretch the mask to the same size as the input pdf page size)
    – Tim
    Commented Feb 17, 2015 at 19:00
  • 1) nope, i think it is correct 2) i'll have a look 3) I think as (vaguely) covered in the last bit of my answer, you lost info when converting the PDF to images - so wwhen converting back to PDf, you lost the scaling and the fact in the original PDF the orignal PDF is over the edge of the PDF's page and is not visible.
    – Wilf
    Commented Feb 17, 2015 at 19:06
  • (1) How do you understand "black in a mask is transparent, while white is opaque, so all we need to do is draw black over anything we don't want visible"? Black is transparent, so draw black over anything we want visible instead? (3) pdftk is said to automatically stretch mask to the same size of the other input pdf page size. "This page is scaled and rotated as needed to fit the input page." from its official manual.
    – Tim
    Commented Feb 17, 2015 at 19:12
  • 1) No, make bits covered in black transparent, bits in white opaque - so the bits covered in black are hidden leaving the white. I haven't got anywhere with pdftk as you need to jump through a few hoops to install/use it on Fedora 21 x64 (packages will seem to only work with Fedora 20...) - I will be able to check stuff later on a Ubuntu machine.
    – Wilf
    Commented Feb 17, 2015 at 19:33

You must log in to answer this question.

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