ImageMagick is an open-source, cross-platform image processing suite. It is a ready-to-run binary distribution that a user can utilize to modify, copy, or distribute in all free/proprietary applications.
Another main advantage of this utility is that it incorporates several threads to improvise computational performance to read, write, or process images of various sizes.
The capability of this utility isn’t limited to image conversion only. It can also edit and create images into various image file formats. Unlike other image processing suites, it performs bulk or repetitive imaging operations and runs at the backend of various web servers, content management systems, and social media networks.
To begin with, update the repository to download the latest available version.
Convert PNG, JPEG Image Format to PDF
First, cd into the image’s directory and list all the images.
Image01.png Image02.png Image03.png
The above output shows all images in the PNG format. Note that we can apply the same image conversion operations on the jpeg file format.
The convert command is the ImageMagick powerhouse, as it’s used for image conversion in all possible formats along with other tasks like image resizing, blurring, drawing, cropping, resampling, and many others.
Let’s list all the possible methods the utility provides with the “convert” command for image conversion to PDF:
- Convert one image file to PDF
To convert an individual PNG file to PDF, run the following command:
convert-im6.q16: not authorized `Image1.pdf' @ error/constitute.c/WriteImage/1037.
The above command may generate an error, as shown above, due to the changes in the ImageMagick global policy file (policy.xml). These changes were recommended as a mitigating step towards an ImageTragick vulnerability for versions below 7.0.1-2.
Go to the /etc/ImageMagick-6/ directory and open the policy.xml file in your favorite editor.
ubuntu@ubuntu:/etc/ImageMagick-6$ sudo vim policy.xml
Change the rights in the following policy from “none” to “read|write”:
Run the “convert” command again and list the directory to find a new pdf file.
- Convert Multiple Images to PDF
The “convert” command also allows converting multiple or all images within the folder to a pdf file. Use the following command to convert multiple images to pdf:
Moreover, ImageMagick extends the Linux filename globbing concept. This feature refers to all the image files in the folder for file conversion.
The above command generates a PDF file containing all the PNG format images of the current folder.
However, ImageMagick does not offer lossless conversion for large-size images with minimum time and memory usage. The drawback leads us to another command-line utility that doesn’t require the user to re-loosen ImageMagick security regulations.
img2pdf is an image conversion utility for raster images to PDF. It’s best suited for the requirements of:
- lossless image conversion: images in PDF with exact color details for each pixel
- fast: does not require CPU to re-encode pixel data
- small: the only file size difference between input and output is due to the PDF container overhead
Another advantage of img2pdf is that it does not load raw pixel details to the memory. This quality feature enables the utility to easily handle large-size input.
Update ubuntu repository to install img2pdf.
The method to convert images to pdf for this utility is somehow similar to ImageMagick. Go to the stored images directory cd /tmp/Images to:
- Convert a single image to PDF
- Convert multiple or all files to PDF
List all the files in the folder to notice newly generated PDF files.
Img2Pdf vs. ImageMagick
Create a large-size test image to compare the lossless-fast conversion of both utilities.
Convert the above PNG image to PDF while using both utilities. Use the Linux time command to note the elapsed time taken by the utilities.
4.15user 0.35system 0:04.99elapsed
ubuntu@ubuntu:/tmp/Images$ sudo time convert image.png ImageMagick.pdf
6.32user 0.33system 0:09.22elapsed
Now open the images to compare both PDFs with the original.
Another way for lossless output in ImageMagick is to use Zip compression, but this method increases the output size.
The article summarizes Linux command-line utilities for image conversion from any image file format to PDF. We introduced ImageMagick and img2pdf as well as discussed their pros and cons to provide a better overview for the readers to choose preferred utility.