简体   繁体   English

在 imagemagick 中转换为 pdf 之前对文件进行排序

[英]sort files before converting to pdf in imagemagick

I have a folder full of image files I need to convert to a pdf.我有一个文件夹,里面装满了我需要转换为 pdf 的图像文件。 I used wget to download them.我使用 wget 下载它们。 The problem is the ordering linux gives the files isn't the actual order of the pages, this is an example of the file ordering:问题是 linux 给文件的排序不是页面的实际顺序,这是文件排序的一个例子:

100-52b69f4490.jpg
101-689eb36688.jpg
10-1bf275d638.jpg
102-6f7dc2def9.jpg
103-2da8842faf.jpg
104-9b01a64111.jpg
105-1d5e3862d8.jpg
106-221412a767.jpg
...

I can convert these images to a pdf using imagemagick, with the command convert *.jpg output.pdf我可以使用 imagemagick 将这些图像转换为 pdf,使用命令convert *.jpg output.pdf

but it'll put the pages into that pdf in the above order, not in human readable numerical order 1-blahblahblah.jpg, 2-blahblahblah.jpg, 3-blahblahblah.jpg etc.但它会按上述顺序将页面放入该 pdf 中,而不是以人类可读的数字顺序 1-blahblahblah.jpg、2-blahblahblah.jpg、3-blahblahblah.jpg 等。

Is the easiest way to do this pipe the output of sort to convert?执行此操作的最简单方法是将排序输出进行转换吗? or to pipe my wget to add each file as I'm getting it to a pdf file?或者在我将每个文件添加到 pdf 文件时通过管道传输我的 wget 以添加每个文件?

There are several options:有几种选择:

The simplest is as follows, but may overflow your command-line length if you have too many pages:最简单的如下,但如果页面太多,可能会溢出命令行长度:

convert $(ls *jpg | sort -n) result.pdf

Next up is feeding the list of files on stdin like this:接下来是在stdinstdin文件列表,如下所示:

ls *jpg | sort -n | convert @- result.pdf

convert $(ls -1v *.jpg) book.pdf

为我工作

Here one bash script to do it that:这里有一个bash脚本来做到这一点:

#!/bin/bash
sort -n < list.txt > sorted_list.tmp
readarray -t list < sorted_list.tmp
convert "${list[@]}" output.pdf
rm sorted_list.tmp
exit

You can get list.txt by first listing your directory with ls > list.txt .你可以list.txt通过首先列出目录ls > list.txt The sort -n (numerical sort) "normalizes" your entries. sort -n (数字排序)“规范化”您的条目。 The sorted list is saved in the .tmp file and deleted at the end.排序后的列表保存在.tmp文件中,最后删除。 Greetings,你好,

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

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