ImageMagick(1) 转换PDF文件至图像

OS: Win10,Linux
相关软件:
(1)cmder(WIndows使用)
(2)ghostscript(转换PDF需要调用,WIndows好像不需要,Linux通过apt install ghostscript即可)
(3)ImageMagick ,官网下载或者apt下载安装


2019-05-26更新

在Ubuntu18.04中使用convert命令转换pdf会报如下错误:

$ convert -verbose -density 300 blog.pdf demo.png
convert-im6.q16: not authorized `blog.pdf' @ error/constitute.c/ReadImage/412.
convert-im6.q16: no images defined `demo.png' @ error/convert.c/ConvertImageCommand/3258.

网址:https://github.com/ImageMagick/ImageMagick/issues/1310 最后的回答给出了解答方案:编辑文件/etc/ImageMagick-6/policy.xml第76行,修改为

<policy domain="coder" rights="read|write" pattern="PDF" />

在这里插入图片描述
本文实现使用ImageMagick中的convert命令将PDF文件转换为图像文件(png,jpg等),需要掌握的基本工具/命令有sort和convert。

convert

先介绍convert命令,详细可以参照官网,网站https://imagemagick.org/script/examples.php 列出了convert的不同例子。
命令行选项有几种不同的形式:

  • Image Setting
    在这里插入图片描述
  • Image Operator
    在这里插入图片描述
  • Image Channel Operator
    在这里插入图片描述
  • Image Sequence Operator
    在这里插入图片描述
  • Image Geometry
    在这里插入图片描述

-append:从上至下合并图像(Image Sequence Operator)
+append:从左至右合并图像(Image Sequence Operator)
-flip: 垂直翻转
-flop: 水平翻转

convert test.pdf demo.png

如果pdf文件有n页的话,前面步骤会生成n张图片,图片名称分别为demo-0.png,demo-1.png,…,demo-(n-1).png,通过默认参数得到的图像分辨率为72dpi(dots per inch),可以通过修改参数-density 实现:

convert -density 300 test.pdf demo.png

300dpi分辨率已经可以接受了。

如果想要转换pdf中的某一页或者部分页,可以执行下面命令:

convert -density 300 test.pdf[0] demo.png

test.pdf[0]表示第一页,注意变换是从0开始的。

sort

前面得到n张demo-开头的文件,如果直接通过命令

convert demo-*.png -append all.png

其顺序会无法保证,得不到想要的效果,需要先人为进行排序,Linux和Win下操作游戏不同。

Linux

建议参考该命令手册:man sort

ls *png | sort -nbr -k 1.5 |convert @- -append all.png

sort的常用参数及作用:
-n:根据数值排列顺序
-b:忽略开头空白字符
-d:只考虑空白和字母顺序
-r:反向顺序
-k POS1,POS2:
引用一段话进行解释该参数:

where POS1 is the starting field position, and POS2 is the ending
field position. Each field position, in turn, is defined as: F.C
…where F is the field number and C is the character within that
field to begin the sort comparison.

使用key参数,可以用来利用不同的field来排列,每个field以空格隔开,像前面文件名demo-0.png,这里只有一个field,我想在这个field的第6个数值字符开始为排列依据,忽略前面5个字符,那么参数就为-k 1.5,表示排列依据为第一个field第6个字符开始,多个filed的情况一般出现在文件中的数据排列,例如有一个文件data.txt,其里面的数据有:

01 Joe Sr.Designer 
02 Marie Jr.Developer 
03 Albert Jr.Designer 
04 Dave Sr.Developer

这样就可以通过参数k实现不同field(每个空格间隔的字符串为一个field)的排序

Windows

Windows环境中笔者使用的是cmder终端,其命令如下:

ls *.png | sort /+7 | convert @- -append all.png

因为在Windows中调用的是windows系统自检的sort命令,其语法在官网网址:https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb491004(v=technet.10) 进行了详细介绍,/+7表示从第7个字符开始作排序比较,Linux下是第6个开始比较,这点是有差别的。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ImageMagick是功能强大的图像处理工具,它可以用来将PDF文件转换为多页图片。以下是使用C语言调用ImageMagick实现PDF多页图片的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <wand/MagickWand.h> int main(int argc, char **argv) { MagickWand *magick_wand; MagickBooleanType status; MagickPixelPacket pixel; PixelIterator *iterator; ExceptionInfo *exception; char *input_file, *output_file; int width, height, page_count, i; if (argc != 4) { printf("Usage: %s input_file output_file resolution\n", argv[0]); return 1; } input_file = argv[1]; output_file = argv[2]; width = atoi(argv[3]); height = width * 1.414; page_count = 0; MagickWandGenesis(); exception = AcquireExceptionInfo(); magick_wand = NewMagickWand(); status = MagickReadImage(magick_wand, input_file); if (status == MagickFalse) { CatchException(exception); printf("Failed to read image: %s\n", exception->reason); return 1; } page_count = MagickGetNumberImages(magick_wand); for (i = 0; i < page_count; i++) { status = MagickSetIteratorIndex(magick_wand, i); if (status == MagickFalse) { CatchException(exception); printf("Failed to set iterator index: %s\n", exception->reason); return 1; } status = MagickResizeImage(magick_wand, width, height, LanczosFilter, 1.0); if (status == MagickFalse) { CatchException(exception); printf("Failed to resize image: %s\n", exception->reason); return 1; } status = MagickSetImageFormat(magick_wand, "JPEG"); if (status == MagickFalse) { CatchException(exception); printf("Failed to set image format: %s\n", exception->reason); return 1; } status = MagickWriteImage(magick_wand, output_file); if (status == MagickFalse) { CatchException(exception); printf("Failed to write image: %s\n", exception->reason); return 1; } } magick_wand = DestroyMagickWand(magick_wand); exception = DestroyExceptionInfo(exception); MagickWandTerminus(); printf("PDF转换为多页图片成功!\n"); return 0; } ``` 上述代码使用MagickWand库来调用ImageMagick实现PDF多页图片。具体来说,它首先读取PDF文件,然后遍历每一页,将其调整为指定的大小并转换为JPEG格式,最后将其写入输出文件中。 使用上述代码需要安装MagickWand库和ImageMagick软件。在Ubuntu系统中,可以使用以下命令来安装它们: ```shell sudo apt-get install libmagickwand-dev imagemagick ``` 在Windows系统中,可以从ImageMagick官网下载二进制安装包并安装。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值