Tuesday, April 12, 2022

Convert images in batch using free tools then make PDF from them.

I was trying to collect public domain PDFs published by Gita Press Gorakhpur from archive.org but the sizes of pdfs are huge. They are unnecessarily high in size. Instead of pdf I downloaded JP2 files and batch converted them to jpg in lower quality setting without loosing any readability. Using this saved me huge space. If published PDF was of 2GB mine was <100MB and perfectly readable.


Above was an example to convert images in batch using free tools. Below are steps to do the same.


- Download ImageMagick-7.1.0-29-Q16-x64-static.exe from https://imagemagick.org/script/download.php

- Install it and try to run magick.exe from cmd.exe. cmd can be run from search box in windows 10.

- Put all images in some folder for example IMAGES. Open cmd.exe and change directory to the IMAGES folder. To do this copy address of this folder from file properties or address bar of windows file explorer. Go to cmd and type cd CTRL+V. Now type drive letter with colon. Example cd D:\folder1\folder2\IMAGES and then D:. This will move us inside the IMAGES folder.

- Type dir command and it should list all images. This shows we are inside the IMAGES folder.


Commands to convert images. Suppose images are jp2 files downloaded from archive.org and we want to convert them to jpg format inside JPG folder in IMAGES. Create JPG folder and run below command.


magick.exe mogrify -path JPG -quality 25% -resize 1500x1500 -format jpg *.jp2


quality option can be removed if not required. I needed it to reduce jpg size. Now it will take time and convert images to jpg format in JPG folder. Now convert these all JPGs to PDF with this command. Change directory to JPG folder using command cd JPG.


magick convert *.jpg -quality 25% 1.pdf


quality option can be removed. I just kept it in case various size PDF are to be tested.


To automate the process make a text file batch_convert.bat and paste below text into it.


mkdir jpg

magick.exe mogrify -path jpg -quality 25% -resize 1500x1500 -format jpg *.jp2

cd jpg

magick.exe convert *.jpg 1.pdf


Copy this file into folder which contains all jp2 files. Run this file double clicking it. When everything works fine it creates a PDF file 1.pdf. All jp2 and jpg files can be deleted later if not needed. To change quality of pdf -quality % value in bat file can be increased. If not sure just use 90%. Its default in most of JPG files.


Instead of using image magick we can also use Fast Stone Image viewer from https://www.faststone.org/FSView.erDetail.htm. This is really good image viewer and has batch conversion, rename tools. Go to Tools - Batch convert images to convert images in batch if you don't like ImageMagick. However ImageMagick is fast and has lots of options and works on Linux, Mac, Android etc. Learning to work with it will make you familiar with multiple OSes.