Page 1 of 1
Convert pdf to png: ignoring -background?
Posted: 2011-08-30T07:41:13-07:00
by Knorkebrot
Hi,
I have a transparent pdf and want to convert it to a non-transparent png using `convert -background white in.pdf out.png`, but the resulting png is still transparent. I tried to use '-flatten', but this will print all pages onto a single one. Also the '-background' option works only when using '-flatten'.
Without flatten this would create one png for each page in the pdf (out-0.png, out-1.png etc.).
I've tried to get this working for hours and I just don't get what I'm doing wrong.
This is a sample PDF:
http://media.kbct.de/zone/serial_letter.pdf
> convert --version
Version: ImageMagick 6.7.1-1 2011-08-29 Q16
http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP
EDIT: This worked without any options using IM 6.6. All PNGs had a white background.
All help is greatly appreciated. I'm in an awkward situation, because - well - I need this since yesterday and I don't get any further...
Re: Convert pdf to png: ignoring -background?
Posted: 2011-08-30T09:59:09-07:00
by fmw42
PNG does not support multiple pages. That is why you get -0 and -1 files. So you need to use GIF or TIFF.
convert serial_letter.pdf -alpha off serial_letter.tif
or
convert serial_letter.pdf -alpha off serial_letter.gif
you can control the quality with supersampling, for example
convert -density 288 serial_letter.pdf -alpha off -resize 25% serial_letter.gif
where 288=72*4. But it will be slower due to the 4x enlargement in each dimension and then reduction by 4x.
Re: Convert pdf to png: ignoring -background?
Posted: 2011-08-30T10:31:46-07:00
by Knorkebrot
You misunderstood my question: it's not about the multiple output files, I want and need them, it's about getting _transparent_ PNGs when I want them to have a white (not transparent) background. :)
But thank you for answering, I already tried '-alpha off', but it produces unreadable text of course. My goal is to have alpha on for smooth and better readable text and a white background, so it's like printed on a white sheet of paper.
Re: Convert pdf to png: ignoring -background?
Posted: 2011-08-30T11:41:50-07:00
by fmw42
The problem is that the ghostscript device pngalpha can only handle transparency in a pdf with one page. So with multiple pages it loses your transparency which causes the anti-aliasing to go away. If you change the ghostscript device to pnmraw, then it can handle multiple pages but without transparency (ignores transparency?). But your anti-aliasing is coming from the transparency and just flattening to white is not really going to do quite as good a job
Best I can suggest, which is not as good as the original is:
convert serial_convert -density 288 serial_letter.pdf[0] -background white -flatten -resize 25% serial_letter_0.png
convert serial_convert -density 288 serial_letter.pdf[1] -background white -flatten -resize 25% serial_letter_1.png
You might get better quality be supersamping even higher (or resizing by 50% or not at all)
Re: Convert pdf to png: ignoring -background?
Posted: 2011-08-30T12:41:49-07:00
by Knorkebrot
So it's not possible to write the image (with alpha) onto a white background (in one command)? It's what I thought '-background' would do
before posting here.
What I'm doing now, isn't that nice since there is one call of convert per page:
Code: Select all
#!/usr/bin/perl -w
# ....
my $pdf = $ARGV[0];
my $name = $pdf;
$name =~ s/^(.*)\.pdf$/$1/; # remove file extension
my $pages = `gs -q -dNODISPLAY -c "($pdf) (r) file runpdfbegin pdfpagecount = quit"`;
$pages--;
for (0..$pages) {
`convert -density 288 "${pdf}\[$_]" -flatten -resize 25% ${name}_$_.png`;
}
The inital `convert serial_letter.pdf serial_letter.png` takes about a tenth of the time the new commands take now.
I'm sorry if I'm a little bit difficult
//EDIT
Ok, if I remove '-density' and '-resize' options it's very fast and the size is ok, I think I'll just do it like that.
Thank you very much, I didn't know I could select specific pages of a pdf, this helped very much.
Re: Convert pdf to png: ignoring -background?
Posted: 2011-08-30T17:03:28-07:00
by anthony
The other way to speed things up is to DIY the ghostscript call
http://www.imagemagick.org/Usage/text/#ghostscript
IM has to go through a lot of 'security hoops' as part of its handling of ghostscript which includes a lot of duplication of IO to disk. Not much can be done about that unfortunately. So if speed is a problem try that and 'pipeline' ghostscript output into IM for further processing to reduce disk IO.
Re: Convert pdf to png: ignoring -background?
Posted: 2011-09-01T09:03:02-07:00
by Knorkebrot
Maybe I will need this later, some customers said they want to adjust the size of those images*, so I'll keep that in mind, thank you
(* the PNGs just serve as preview of the later downloaded or printed vouchers/invoices/delivery orders etc.)