Issue
I frequently have to merge a lot of images inside a pdf. Naturally, I'd like to automate this a bit.
Here are the things I need:
- lossless merge into a pdf
- merge of most common image formats
- natural sorting of numbers (
1.jpg
,2.jpg
, and10.jpg
would be merged in this order, not as1.jpg
10.jpg
2.jpg
) - can deal with white-spaces in file names
I've come across a solution that works exactly like I'd need it to, but which fails for files containing a white-space in their name.
#!/bin/bash
convert `ls -v *.jpg *.jpeg *.JPG *.JPEG *.png *.PNG *.gif *.GIF *.tiff *.TIFF *.webp *.WEBP *.bmp *.BMP` compilation_images.pdf
Solution
Trying to parse ls
is wrong, as you've found out. Assuming you're using GNU versions of utilities as the linux tag usually implies, try something like:
#!/usr/bin/env bash
# Turn on case-insensitive globs and make ones that
# don't match anything expand to nothing
shopt -s nocaseglob nullglob
readarray -d '' -t images < <(printf "%s\0" *.jpg *.jpeg *.png *.gif *.tiff *.webp *.bmp | sort -Vz)
convert "${images[@]}" compilation_images.pdf
which uses the common trick of separating filenames with nul characters in order to safely handle whitespace when populating a bash
array with sorted filenames.
Answered By - Shawn Answer Checked By - Dawn Plyler (WPSolving Volunteer)