Converting PNG files to single PDF with 2 per row

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
fronty
Posts: 3
Joined: 2017-12-07T10:23:12-07:00
Authentication code: 1152

Converting PNG files to single PDF with 2 per row

Post by fronty » 2017-12-07T10:44:50-07:00

Hi all,

I have spent all day fiddling with ImageMagick and am really struggling, hope someone can help.

I have 38 PNG files, each image is 600x230 in size. I need to put these into a single PDF.

If I do:

convert -page A4 *.png fronty.pdf

I can get them all into a PDF, but only 1 image per page and at 100% zoom factor in Acrobat the image fills the page horizontally. If I do:

convert -append -page A4 *.png fronty.pdf

It shrinks the images so they all fit on one page (I don't want them shrinked!). It would be nice if I could get them tiled, 2 across and 4 rows, so 8 images per page.

I found the -tile option but I couldn't get it to work with compress. Then I found this:

montage -tile 2x *.png fronty.pdf

This is getting me closer, I now have 2 images per row, but now there's no pagination, it's just a big long PDF with no page breaks, and there's an arbitrary gap between each row which I need to make smaller. If I add -page A4 it shrinks so they're all on one page.

But the main problem is that the image quality is awful, it's like it's really compressed the images down and all the detail has been lost. I've been trying to switch off lossless compression (+compress and -compress Zip), and mucked around with the -quality and -resample options, and -density but getting nowhere.

Can someone help me figure this out, I just want two images per row, with a customisable gap, and for the PDF to be paginated. Any idea?

Thanks.

User avatar
fmw42
Posts: 22671
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting PNG files to single PDF with 2 per row

Post by fmw42 » 2017-12-07T11:04:36-07:00

Putting raster images into a PDF, just embeds the raster files in a vector shell. The quality will be no better than the size of the input raster images.

Something like this works fine for me using the 640x480 IM internal image logo: to make a 3 page pdf. See http://www.imagemagick.org/script/forma ... tin-images

Code: Select all

convert logo: logo: logo: -gravity center -background white -extent 595x842 -adjoin logo.pdf

fronty
Posts: 3
Joined: 2017-12-07T10:23:12-07:00
Authentication code: 1152

Re: Converting PNG files to single PDF with 2 per row

Post by fronty » 2017-12-07T15:40:37-07:00

Thanks for replying so quickly, so in your example there is one logo per page, how would you make it so 2 logo's appear on the same page in a row?

Through lots of fiddling I've found that if I add a geometry statement it doesn't give me the "compressed" images any longer, this statement works pretty well:

montage -tile 2x -geometry +4+4 *.png fronty.pdf

but I have no idea why it makes such a difference.

User avatar
fmw42
Posts: 22671
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting PNG files to single PDF with 2 per row

Post by fmw42 » 2017-12-07T16:18:47-07:00

That command is going to put all image in a single page stacked 2 in a row. It won't make multiple pages such that each page has only two images side-by-side. To do that you would have to write a script loop over all your images and combine them in pairs using montage if you want or append writing each pair to a new file, then combine the files into multiple page pdf.

snibgo
Posts: 9745
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting PNG files to single PDF with 2 per row

Post by snibgo » 2017-12-07T16:57:23-07:00

When IM outputs to PDF, it writes one image per page. If you want each page to contain multiple images first, you need to combine them.

For example, if r.tiff contains 38 images ...

Code: Select all

montage r.tiff -tile 4x2 r.pdf
... then r.pdf will contain 5 pages. The first 4 pages have 8 images from r.tiff, arranged as 4x2. The last pdf page has only 6 images.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22671
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting PNG files to single PDF with 2 per row

Post by fmw42 » 2017-12-07T17:04:31-07:00

According to snibgo, then this modification to your command should work to make multiple pages with 2 images side by side. I was wrong about needing to loop over your images and snibgo was correct. You just needed to modify -tile 2x to -tile 2x1

Code: Select all

montage -tile 2x1 -geometry +4+4 *.png fronty.pdf

fronty
Posts: 3
Joined: 2017-12-07T10:23:12-07:00
Authentication code: 1152

Re: Converting PNG files to single PDF with 2 per row

Post by fronty » 2017-12-08T02:56:43-07:00

Ok, I'm getting there, this command fits all the images on an A4 page and then creates additional pages as required. It would be nice if I could get the page number on each page somehow, I haven't found a way to do that yet. Also the images go right up to the edges of the page, it would be nice if I could specify a left, right, top and bottom margins, if I fiddle with the geometry statement I get more spacing between the images which I don't want, so just trying to figure that out at the moment...

Code: Select all

montage -title "Capacity Report" -tile 2x7 -geometry +5+5 -page A4 *.png fronty.pdf

snibgo
Posts: 9745
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting PNG files to single PDF with 2 per row

Post by snibgo » 2017-12-08T04:06:56-07:00

After the "-tile", you have one image for each of the output pages. You can do whatever you like to those images, such as adding a white border or annotating with a page number. I don't use montage so can't advise on that, but of course you could write the output to miff and pipe that to convert, then do whatever you want before finally writing to pdf.
snibgo's IM pages: im.snibgo.com

Post Reply