imagemagick的convert指令转换jpg,png为pdf

使用ComicShelf软件下载漫画

首先安装ComicShelf这个软件。

使用ComicShelf下载所需要的漫画书到本地目录。

这里我下载了《妖精的尾巴》,而后拷贝到 /tmp 下面。

按每10卷整理漫画

来看看 /tmp 目录下的文件结构。

superman@superman-laptop:/tmp$ tree /tmp/妖精的尾巴|head -n 100
/tmp/\345\246\226\347\262\276\347\232\204\345\260\276\345\267\264
├── 011-020
│   ├── 011\350\257\235
│   │   ├── 001.png
... ...
│   │   ├── 022.jpg
│   │   └── 023.jpg
│   ├── 012\350\257\235
│   │   ├── 001.png
... ...
│   │   └── 025.jpg
... ...
│   ├── 015\350\257\235
│   │   ├── 001.png
│   │   ├── 002.png
... ...

在“/tmp/妖精的尾巴”目录下,我将漫画按照每10卷一个目录存放。

第一级目录是“011-020卷”、“021-030卷”等等;

第二级目录是011卷、012卷、013卷等等对应各卷漫画;

第三级就是每一卷漫画下面的具体漫画图片了。

编写shell脚本每10卷压缩为一个PDF文件

find /tmp/妖精的尾巴 -maxdepth 1 | awk -F / '  {if ($4!="") system("find "$1"/"$2"/"$3"/"$4" | sort > /tmp/tmp.txt;  cd /tmp; convert -quality 100 -units pixelsperinch  -density 60x60 -depth 16 -colorspace RGB  -interlace none -verbose @tmp.txt /tmp/"$3"-"$4".pdf")}'

语句1:

find /tmp/妖精的尾巴 -maxdepth 1

通过这个指令可以得到如下路径信息:

/tmp/妖精的尾巴
/tmp/妖精的尾巴/051-060
/tmp/妖精的尾巴/141-150
/tmp/妖精的尾巴/131-140
/tmp/妖精的尾巴/091-100
/tmp/妖精的尾巴/231-240
……

语句2:

awk -F / '...'
 

在awk里,用“/”作为分隔符。举例而言:

示例1:

/tmp/妖精的尾巴/071-080

对应的分解结果:

$1 (空字符串)

$2 tmp

$3 妖精的尾巴

$4 071-080

 

语句3:

if ($4!="")

后来想想这句判断有点多余,至于为什么,大家自己试试看就知道了。

语句4:

按上面分解的结果,不难知道system中拼接的指令字符串最终会是

find /tmp/妖精的尾巴/071-080 \
                      | sort > /tmp/tmp.txt; \
                      cd /tmp; \
                      convert -quality 100 -units pixelsperinch \
                                  -density 60x60 -depth 16 -colorspace RGB \
                                  -interlace none -verbose 
                                  @tmp.txt /tmp/妖精的尾巴-071-080.pdf
 

看出来了吧,就这样对每10卷一个目录的所有图片进行排序后,利用imagemagick的convert指令,进行向pdf格式的转换操作。

至于quality, density, depth这些参数的含义,我就不多说了,好像density是对应dpi的,值小一些会让图片更大一点。

convert的详细参数可以参考 【convert options官方文档】。

后记

由于imagemagick在安装的时候,默认引入了对libgomp的依赖,而这个提供线程支持的API包在某些linux下是有问题的,会报如下错误:

英文版本提示信息可能是:

libgomp: Thread creation failed: Cannot allocate memory


中文版本提示信息则是:

libgomp: Thread creation failed: 资源暂时不可用

所以也在考虑使用GraphicsMagick改写使用imagemagick来做转换的功能代码。

猜你喜欢

转载自gkbusy.iteye.com/blog/1153712