Composite PDF with opaque backgrounds

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
matejaputic
Posts: 4
Joined: 2016-11-13T14:01:23-07:00
Authentication code: 1151

Composite PDF with opaque backgrounds

Post by matejaputic »

I would like to create a composite of all pages in a PDF to use as a tool for determining crop margins for cutting out page numbers and headers, etc. So far this has been working well:

> composite -gravity center mydocument.pdf mydocument-composite.pdf

This works fine for PDFs with transparent backgrounds, but I'm having trouble with PDFs with opaque backgrounds. How can I process the PDF to remove or make white backgrounds transparent, and then create a composite of all the pages?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composite PDF with opaque backgrounds

Post by snibgo »

What version IM are you using? On what platform?

I would use "convert", not "composite".

"-transparent White" will turn white pixels transparent. But I think you want to make one image that is black where any of the pages are black. "-compose Darken" will do that.
snibgo's IM pages: im.snibgo.com
matejaputic
Posts: 4
Joined: 2016-11-13T14:01:23-07:00
Authentication code: 1151

Re: Composite PDF with opaque backgrounds

Post by matejaputic »

I am using ImageMagick 6.9.5-4 Q16 x86_64 2016-07-30 on OSX 10.10.5

I apologize I wasn't clear, I need two things:

1. Convert a PDF with white backgrounds into a PDF (or PNGs or any other format) with transparent backgrounds
2. Composite the pages of the PDF with transparent backgrounds into one image or one PDF

I am converting the PDF to an e-reader format and I want to be able to see how large I need to set the crop margins so that the page numbers and headings on each page get cut out. Looking at the composite image makes this easy because it's like looking at all the pages in the PDF at once. I need help doing step 1. I have already tried

> convert -transparent White mydocument.pdf

which does not work. Does anyone have any ideas how to do this?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite PDF with opaque backgrounds

Post by fmw42 »

If the pdf has multiple pages, then try

Code: Select all

convert -density DD image.pdf -transparent white -background none -flatten image.png
Where DD is the dpi you want to use to convert the vector pdf to raster data. Use 72 if you do not want to enlarge the output.

If the white is not pure white, then add -fuzz XX% before -transparent


But perhaps you should provide and example of your PDF, so we can experiment with it. You can post to some place such as dropbox.com and put the URL here.
matejaputic
Posts: 4
Joined: 2016-11-13T14:01:23-07:00
Authentication code: 1151

Re: Composite PDF with opaque backgrounds

Post by matejaputic »

Here is an example of a paper that I processed successfully

Original: http://cs.stanford.edu/~zdevito/a144-hegarty.pdf
Processed: https://app.box.com/s/wv7hnwf1fda78kq3hp46x5kxiakpnu2f

which was processed with

Code: Select all

composite -gravity center a144-hegarty.pdf a144-hegarty-composite.pdf
This is the output that I want. All of the pages are composited into one, and I can easily measure how large I have to set the crop margins so that the headers and footers from each page are removed.

Here is an example of a paper that did not work:

Original: https://pdfs.semanticscholar.org/82fd/a ... 0a4330.pdf

With the same command as above, the output looks like this:

Processed: https://app.box.com/s/8byjky1ovp7po3kblr9td1hqqon1oaez

I have also tried many variations of the commands suggested by fmw42 and snibgo (thank you) with no luck. I have tried including -transparent-color white, -transparent white, -background none, etc. Nothing seems to work.

Does the first document work but the second one doesn't because the first document does not have a background color?
Could it be that they are different PDF formats, e.g., one is native and the other might be images of pages?

I would appreciate your guidance, thanks.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Composite PDF with opaque backgrounds

Post by snibgo »

Calling your PDF "neural.pdf":

Code: Select all

convert neural.pdf -compose Darken -layers Merge d.png
Doesn't that do what you want?
snibgo's IM pages: im.snibgo.com
matejaputic
Posts: 4
Joined: 2016-11-13T14:01:23-07:00
Authentication code: 1151

Re: Composite PDF with opaque backgrounds

Post by matejaputic »

That's it! Thank you so much!
Post Reply