Saturday, 23 November 2013

How-to: Convert JPG to PDF using ImageMagick


It's a common enough task, trying to convert multiple jpg files into one pdf, particularly when I don't need these to be converted with such high quality, I just want the black and white text readable.

I can scan, crop, and monochrome in a graphics program but compiling them into a single PDF booklet was always tricky. Which is how I started using ImageMagick.

ImageMagick is a command line conversion program that is capable of so many more batch operations than this - resizing, compression, format conversion - and it's available on all platforms - Linux, Windows and Mac.


Bear in mind that creating a PDF document from multiple JPEG images can take some time and you may want to trial different settings for size and quality of output, so I suggest you make a copy of the JPEG files in a temporary folder to play around with and use Imagemagick on those, NOT your originals. Reducing the resolution as a first step will also make things much quicker.

Originally I was just using the basic
convert *.jpg output.pdf and/or convert *.jpg -adjoin output.pdf

which works most of the time, however there's a bug in the convert routine which can in some versions give a segmentation fault when converting a number of JPEG files to one PDF file.

What this command does is take all the .jpg's (or format of your choice) in a folder and convert them to a single PDF - you can name it whatever you like.

You can avoid the segmentation fault bug and do the compression at the same time if you use
convert *.JPG -compress Zip output.pdf

but the zip compression appears quite inefficient and results in huge file sizes.

You could resize and lower the quality of the images using;
mogrify -resize 50% -quality 25

Which overwrites the originals. You can combine resizing and conversion using

convert -quality 25 -resize 50% *.jpg -adjoin output.pdf

which works, but takes longer as you're combining batch operations.

And yes, you can resizing images without overwriting the originals by specifying a new file name;
convert '*.JPG' -resize 640x480 newfile%03d.jpg

which outputs the converted images as newfile001.jpg, newfile002.jpg, and so on. Alternatively, if you want to retain the original file name and prepend new, you could use a bit more code:
for file in *.JPG ; do convert "$file" -resize 640x480  "new-${file}" ; done

This is just a sample of what Imagemagick can do if you are prepared to experiment at the command line. RC

11 comments:

  1. Great tip man. This was all that I needed in order to create a pdf. I just changed a parameter "convert *.jpg -compress jpeg output.pdf"
    Worked like a charm.

    ReplyDelete
  2. This resource is very useful! But I also liked this one for the ease of use: Really makes it easy to convert images to PDF. I didn't have to download anything and it was really easy to use. You can also convert all image formats, it's great. It converts JPG to PDF but also PNG, BMP, TIF or GIF

    ReplyDelete
  3. This line converts all files of all sub-directories into single pdfs per sub-directory, maintaining the name of the sub as name of the pdf:

    find . -type d -print1 | xargs -0 -I '{}' convert '{}'/* '{}'.pdf

    ReplyDelete
  4. I find a free online pdf to image converter, you can convert pdf to jpg online free.

    ReplyDelete
  5. I'm not a developer, i always use this free online pdf to image converter to convert pdf to jpg online.

    ReplyDelete
  6. Good article!

    But how about the other way round?

    Do you have any idea how convert from pdf to image?

    I was looking into other forums, seems like ImageMagick is good, and freely available too.

    ReplyDelete
  7. Several ways to skin this particular cat, very much dependent on how your source PDF was created and how the images are embedded/layered in the document.

    On Linux, there's a good command line tool: pdfimages.

    I'll take a look at a basic tutorial some time.

    ReplyDelete
  8. convert image.pdf image.jpg will convert a pdf to an image. You can use -quality et -density to deal with quality.

    ReplyDelete
  9. You will need to download the free new pdf from images filename. download this new pdf from images filename with high quality on page http://www.pdfcoding.com/online/pdf/convert-jpeg-images-to-pdf/.

    ReplyDelete

At least try to be nice, it won't kill you...