Page 1 of 2

Convert PDF to Images without transparent background

Posted: 2016-12-08T10:14:10-07:00
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

Re: Convert PDF to Images without transparent background

Posted: 2016-12-08T10:19:09-07:00
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.

Re: Convert PDF to Images without transparent background

Posted: 2016-12-08T11:02:23-07:00
by LittleKI
doesn't work. i tested it with this file: http://www.joggen-online.de/images/dehn ... online.pdf

Re: Convert PDF to Images without transparent background

Posted: 2016-12-08T11:12:15-07:00
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

Re: Convert PDF to Images without transparent background

Posted: 2016-12-14T05:10:13-07:00
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

Re: Convert PDF to Images without transparent background

Posted: 2016-12-14T05:26:53-07:00
by snibgo
Try a density, eg "-density 300" or 600 or whatever.

Re: Convert PDF to Images without transparent background

Posted: 2016-12-15T03:09:32-07:00
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?

Re: Convert PDF to Images without transparent background

Posted: 2016-12-15T03:17:38-07:00
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.

Re: Convert PDF to Images without transparent background

Posted: 2016-12-15T09:57:40-07:00
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)%

Re: Convert PDF to Images without transparent background

Posted: 2016-12-18T07:08:04-07:00
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?

Re: Convert PDF to Images without transparent background

Posted: 2016-12-18T13:01:46-07:00
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

Re: Convert PDF to Images without transparent background

Posted: 2016-12-29T10:30:37-07:00
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.

Re: Convert PDF to Images without transparent background

Posted: 2016-12-29T10:58:59-07:00
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

Re: Convert PDF to Images without transparent background

Posted: 2016-12-29T15:57:48-07:00
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.

Re: Convert PDF to Images without transparent background

Posted: 2016-12-29T16:45:43-07:00
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