简体   繁体   English

用于合并 pdf 内文件夹中的所有图像文件的脚本

[英]Script to merge all image files from a folder inside a pdf

I frequently have to merge a lot of images inside a pdf.我经常需要在 pdf 中合并很多图像。 Naturally, I'd like to automate this a bit.自然,我想自动化一点。 Here are the things I need:这是我需要的东西:

  • lossless merge into a pdf无损合并到 pdf
  • merge of most common image formats合并最常见的图像格式
  • natural sorting of numbers ( 1.jpg , 2.jpg , and 10.jpg would be merged in this order, not as 1.jpg 10.jpg 2.jpg )数字的自然排序( 1.jpg2.jpg10.jpg将按此顺序合并,而不是1.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

I'm a complete noob when it comes to shell scripting, so I apologise in advance if I failed to see an easy solution, but I was unable to find any by myself.在 shell 脚本方面,我完全是个菜鸟,所以如果我没有看到一个简单的解决方案,我提前道歉,但我自己找不到任何解决方案。

Thanks for your help.谢谢你的帮助。

Trying to parse ls is wrong , as you've found out.正如您所发现的,尝试解析ls是错误的。 Assuming you're using GNU versions of utilities as the linux tag usually implies, try something like:假设您使用的是 GNU 版本的实用程序,因为 linux 标记通常暗示,请尝试以下操作:

#!/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.它使用用 nul 字符分隔文件名的常见技巧,以便在使用排序的文件名填充bash数组时安全地处理空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM