Convert PDF to Images without transparent background

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?".
LittleKI
Posts: 32
Joined: 2015-01-11T11:55:29-07:00
Authentication code: 6789

Convert PDF to Images without transparent background

Post by LittleKI »

Hi,

i use convert to export all the pages of a pdf to images (one image per page). sometimes the pdf documents have transparent background, but i don't want to have this in the images. in okular (on linux mint 18) the background is white. how can i set up that convert exports the images with the same background color that okular displays. i use convert with servicemenus in kde, so the command line is always the same. the solution should work also for pdf documents without transparent background.

is there a settings in the pdf file where the pdf viewer reads out the color of the background or is it set in the okular settings, so that transparent background is always displayed white?

this is the command line:

Code: Select all

convert input -compress zip +adjoin output.tiff
LittleKI
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PDF to Images without transparent background

Post by fmw42 »

try

Code: Select all

convert input.pdf -background white -alpha background -alpha off -compress zip +adjoin output.tiff
If that does not work, the post and example pdf to some place such as dropbox.com and put the URL here.
LittleKI
Posts: 32
Joined: 2015-01-11T11:55:29-07:00
Authentication code: 6789

Re: Convert PDF to Images without transparent background

Post by LittleKI »

doesn't work. i tested it with this file: http://www.joggen-online.de/images/dehn ... online.pdf
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PDF to Images without transparent background

Post by fmw42 »

This worked fine for me on IM 6.9.6.7 Q16 Mac OSX

Code: Select all

convert dehnuebungen-2012-ebook-joggen-online.pdf -background white -alpha background -alpha off -compress zip +adjoin output.tiff
What is your IM version and platform? Please always provide that information when asking questions, since syntax may vary or your version is too old to support new functionality.

Also what is your version of Ghostscript. Perhaps it is too old. I am using GS 9.16
LittleKI
Posts: 32
Joined: 2015-01-11T11:55:29-07:00
Authentication code: 6789

Re: Convert PDF to Images without transparent background

Post by LittleKI »

OK, now it works with KDE Servicemenu.

my IM version is 6.8.9.9, ghostscript is 9.18. i use linux mint 18.

BUT: the output quality is very bad. the letters looks like lego.

EDIT:
seems this works

Code: Select all

convert input -background white -alpha remove -compress zip +adjoin output
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert PDF to Images without transparent background

Post by snibgo »

Try a density, eg "-density 300" or 600 or whatever.
snibgo's IM pages: im.snibgo.com
LittleKI
Posts: 32
Joined: 2015-01-11T11:55:29-07:00
Authentication code: 6789

Re: Convert PDF to Images without transparent background

Post by LittleKI »

seems density is the same option like 'dpi' in other applications? in pdfxchange viewer (that i used on windows to export images from a pdf file) i can set up x-dpi and y-dpi (both 300). whats that in imagemagick?
LittleKI
Posts: 32
Joined: 2015-01-11T11:55:29-07:00
Authentication code: 6789

Re: Convert PDF to Images without transparent background

Post by LittleKI »

i converted 2 pdf files to images, one with convert, one with pdfxchangeviewer. the first is 3000x2000px as the original, the second is 500x400. how can i keep the input resolution for each image (each site of the pdf). i don't want to set up this manually because every pdf file is different.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PDF to Images without transparent background

Post by fmw42 »

density is what determines the resulting raster image file dimension. Nominal density is 72, but if you want larger raster images, then using larger densities. -density should be specified before reading the input pdf. For higher quality results, you can increase the density by a factor of X and then after reading the pdf use -resize by (100/X)%
LittleKI
Posts: 32
Joined: 2015-01-11T11:55:29-07:00
Authentication code: 6789

Re: Convert PDF to Images without transparent background

Post by LittleKI »

Doesn't work. I set density to 1200 before the import, but the output quality is poor. when i zoom in the resulting image the edges are not plain.

the resolution of the output is very small, it only 597x842px. i think this is the problem. i don't know the resolution of the image in the pdf. how can i keep the input resolution for the output?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PDF to Images without transparent background

Post by fmw42 »

What was your exact command line? And provide your input file if not the one above.

Try this with density 4*72=288

Code: Select all

convert -density 288 -units pixelsperinch dehnuebungen-2012-ebook-joggen-online.pdf -background white -alpha background -alpha off -compress zip +adjoin output.tiff
or this with density 4*77=288 and resize = 100%/4 = 25%

Code: Select all

convert -density 288 -units pixelsperinch dehnuebungen-2012-ebook-joggen-online.pdf -background white -alpha background -alpha off -resize 25% -compress zip +adjoin output.tiff
LittleKI
Posts: 32
Joined: 2015-01-11T11:55:29-07:00
Authentication code: 6789

Re: Convert PDF to Images without transparent background

Post by LittleKI »

This is the Command line i tried today

Code: Select all

convert input -density 600 -background white -alpha remove -resize 100% -compress zip +adjoin output
i don't want to resize. the target is that the output images have the same resolution as the input pdf. when i sites in the pdf file have different resolutions, so the output images should also have different resolutions. i thought with 'resize 100%' i can do this, but it doesn't work.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PDF to Images without transparent background

Post by fmw42 »

PDF files are vector files that may contain a raster image. Getting the exact raster resolution for you output is hard unless you remove the raster image from the vector shell. If your PDF is all vector data, then note that vector data has no size until you specify the density, which needs to come before reading the PDF. Also IM is not a good vector to vector processor (PDF to PDF). It will rasterize your vector data.

If you want the same dimensions converting a PDF to TIFF, then typically it would just be

Code: Select all

convert -density 72x72 -units pixelsperinch image.pdf -compress zip +adjoin image.tif
Also not that different viewers will assign a different density to your PDF when displaying. Some use 72 dpi and others use 96 dpi. So you cannot count on matching the pdf to the tiff simply by the viewer.

-resize 100% will not help since it will not change the dimensions after rasterization
LittleKI
Posts: 32
Joined: 2015-01-11T11:55:29-07:00
Authentication code: 6789

Re: Convert PDF to Images without transparent background

Post by LittleKI »

PDF files are vector files that may contain a raster image.
OK, i didn't know this.

But i can try what i want (density 300x300 or higher), the output quality is extremly poor. you can try it with this document. density doesn't improve the output quality. Other Tools like PDF-XChange Viewer (Windows only) can do it, but they can't be controlled with the command line.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PDF to Images without transparent background

Post by fmw42 »

You can certainly use -density 300x300, but it will make the tiff image larger. BUT, you must put -density before reading the pdf file. You can also keep the original resolution equivalent to 72 dpi by supersampling. So either do:

Code: Select all

convert -density 300x300 -units pixelsperinch image.pdf -compress zip +adjoin image.tif
or use density = 72*4 = 288, then resample by 1/4 = 25%

Code: Select all

convert -density 288x288 -units pixelsperinch image.pdf -resize 25% -compress zip +adjoin image.tif
\

Best if you supply an example PDF.

Please see the very first post in this forum at viewtopic.php?f=1&t=9620
Post Reply