Image Processing Using the Command Line in Linux

Articles and examples about processing images using the command line interface.

Tue 11/11/25, 2:24 AM | CS

Creating a PDF File From Photos

If you have a scanner available, a feature for making PDFs could be built-in, either in the scanner or in a scanning application.

What happens to me pretty often is that I have photos of the pages, shot using a cellphone, and later there is a need to send the pages by email, for example. Recently I studied what would be a quick way to convert separate photos to one PDF file.

If the image ratio of the original images is relatively close to A4 paper's ratio (for example, 3:4) and the pages were photographed so that there is a couple of centimeters of empty space around the paper, the following recipe usually works well:

Create a new directory and copy the photos into it (make a copy so that originals will be available for a new conversion with different parameters if the first conversion is not successful).

Change the filenames to reflect the time of photography (optional, but often the filenames created by cellphone become more easy to read):

jhead -n%Y-%m-%d---%H.%M.%S *jpg

Shrink images so that maximum width is 2000 pixels and maximum height also 2000 pixels (but preserve the image ratio):

for i in *jpg; do echo $i; convert $i -resize 2000x2000 $i; done

Put photos into a PDF file whose page size is A4:

img2pdf --output asiakirja.pdf --pagesize A4 --fit fill --auto-orient p1.jpg p2.jpg p3.jpg

Ready. Then we do a quality check on the resulting file.

Thu 11/6/25, 8:13 PM | CS

Batch Convert Images to Another Format

The following converts all webp images in the current directory to jpg images. The file suffix is also changed accordingly.

mogrify -format jpg *.webp

Tue 11/11/25, 3:03 AM | CS

Renaming Photos Using Jhead

JPEG photos usually contain an EXIF record that, among other things, contains a timestamp that tells when the picture was taken. When I transfer photos from camera or phone to a photo archive, I like to rename the files so that it is easy to browse the photos in chronological order. I usually rename the photos using this jhead command:

jhead -n%Y-%m-%d---%H.%M.%S photo.jpg

The above command reads the date and time from the EXIF record and changes the filename so that it starts with year, month, and day, followed by hours, minutes, and seconds. Some examples of the resulting filenames:

2021-03-22---20.00.00.jpg
2021-03-22---20.01.22.jpg
2021-03-22---20.02.35.jpg
2021-03-23---20.01.14.jpg
2021-03-23---20.01.48.jpg

(Photos taken on March 22 and March 23, around 8pm.)

If all photos in a directory are renamed in this fashion, most programs and directory listings will automatically show the photos in the natural order, because the alphabetical order of filenames now matches the chronological order.

Thu 11/6/25, 8:15 PM | CS

Image Resizing Using Convert

To shrink an image so that the maximum width is 1200 and maximum height is 1200 but the image aspect is preserved:

convert pic.jpg -resize 1200x1200 pic-hd.jpg

To shrink an image "in place" (original image is overwritten):

convert pic.jpg -resize 1200x1200 pic.jpg

We can extend this to shrink all images in the current directory (it's a good idea to make a backup of the original images first):

for i in *jpg; do echo $i; convert $i -resize 1200x1200 $i; done
Click anywhere to close