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...
Convert pdf to png: ignoring -background?
- Knorkebrot
- Posts: 4
- Joined: 2011-08-29T08:45:06-07:00
- Authentication code: 8675308
Convert pdf to png: ignoring -background?
nene, das gehört so.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert pdf to png: ignoring -background?
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.
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.
- Knorkebrot
- Posts: 4
- Joined: 2011-08-29T08:45:06-07:00
- Authentication code: 8675308
Re: Convert pdf to png: ignoring -background?
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.
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.
nene, das gehört so.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert pdf to png: ignoring -background?
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)
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)
- Knorkebrot
- Posts: 4
- Joined: 2011-08-29T08:45:06-07:00
- Authentication code: 8675308
Re: Convert pdf to png: ignoring -background?
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:
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.
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`;
}
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.
nene, das gehört so.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Convert pdf to png: ignoring -background?
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.
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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- Knorkebrot
- Posts: 4
- Joined: 2011-08-29T08:45:06-07:00
- Authentication code: 8675308
Re: Convert pdf to png: ignoring -background?
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.)
(* the PNGs just serve as preview of the later downloaded or printed vouchers/invoices/delivery orders etc.)
nene, das gehört so.