ImageMagick:将 PDF 转换为 JPG 时尺寸错误

sup*_*iox 3 linux pdf ghostscript imagemagick pdftk

我想对 pdf 应用一些改进(更改亮度、对比度等),使其更具可读性,所以我选择了 ImageMagick 和 pdftk。我用下面的命令把pdf分割成几个单页的pdf文件,这样我就可以一次对ImageMagick一个文件进行操作。

pdftk a.pdf burst output %04d.pdf
Run Code Online (Sandbox Code Playgroud)

此时,一切正常。我使用其中一个文件(例如 0038.pdf)进行测试。例如,为了调整对比度,我使用了这个命令:

convert 0038.pdf -quality 100 -density 300 -brightness-contrast 0x10% out.pdf
Run Code Online (Sandbox Code Playgroud)

但这是结果:

原来的

原pdf

转换

转换后的pdf

我尝试更改质量、密度、大小、调整大小、几何形状的值,输出 pdf 具有不同的大小/分辨率,但始终无法读取。所以我意识到问题出在转换的上游。似乎从 .pdf 文件中错误地读取了输入的 pdf 大小和分辨率convert

事实上,当我输入这个命令时:

convert -verbose 0038.pdf out.pdf
Run Code Online (Sandbox Code Playgroud)

我得到:

/tmp/magick-9894W9c_JPl1I7QV1 PNG 380x482 380x482+0+0 8-bit sRGB 128KB 0.010u 0:00.010
0038.pdf PNG 380x482 380x482+0+0 16-bit sRGB 128KB 0.000u 0:00.000
0038.pdf=>out.pdf PNG 380x482 380x482+0+0 16-bit sRGB 125KB 0.050u 0:00.049
[ghostscript library] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72"  "-sOutputFile=/tmp/magick-9894W9c_JPl1I7QV%d" "-f/tmp/magick-9894s1cR4gD9oYuz" "-f/tmp/magick-9894KMmuVq0n9U8c"
Run Code Online (Sandbox Code Playgroud)

如您所见,大小为 380 x 482,但我知道实际大小为 1653 x 2338 像素。

这是0038.pdf的元数据(阅读exiftool

 ExifToolVersion = 9.70
  FileName = 0038.pdf
  Directory = .
  FileSize = 429577
  FileModifyDate = 1414935360
  FileAccessDate = 1414935693
  FileInodeChangeDate = 1414935693
  FilePermissions = 33188
  FileType = PDF
  MIMEType = application/pdf
  PDFVersion = 1.4
  Linearized = false
PDF dictionary (1 of 1) with 4 entries:
  0)  Info (SubDirectory) -->
  + [Info directory with 5 entries]
  | 0)  ModifyDate = (D:20141101192012Z)
  | 1)  CreateDate = (D:20141101192012Z)
  | 2)  Title = 0038
  | 3)  Creator = (pdftk 2.02 - www.pdftk.com)
  | 4)  Producer = (itext-paulo-155 \(itextpdf.sf.net-lowagie.com\))
  1)  ID = [<e5af52575d23dc1a2aca80f7453fa203>,<4cc6d7fb99aca8c755033ca2973b713c>]
  2)  Root (SubDirectory) -->
  + [Root directory with 3 entries]
  | 0)  Metadata (SubDirectory) -->
  | + [Metadata directory with 3 entries]
  | | 0)  Subtype = /XML
  | | 1)  Type = /Metadata
  | | 2)  Length = 3008
  | | + [XMP directory, 3008 bytes]
  | | | XMPToolkit = Image::ExifTool 9.70
  | | | Title = 0038
  | | | Artist = A
  | 1)  Type = /Catalog
  | 2)  Pages (SubDirectory) -->
  | + [Pages directory with 3 entries]
  | | 0)  Kids (SubDirectory) -->
  | | + [Kids directory with 7 entries]
  | | | 0)  Resources (SubDirectory) -->
  | | | + [Resources directory with 2 entries]
  | | | | 0)  ProcSet = [/PDF,/Text,/ImageB,/ImageC,/ImageI]
  | | | | 1)  XObject (SubDirectory) -->
  | | | | + [XObject directory with 1 entries]
  | | | | | 0)  JI19a (SubDirectory) -->
  | | | | | + [JI19a directory with 9 entries]
  | | | | | | 0)  Subtype = /Image
  | | | | | | 1)  Name = /JI19a
  | | | | | | 2)  Type = /XObject
  | | | | | | 3)  Width = 1653
  | | | | | | 4)  Filter = /DCTDecode
  | | | | | | 5)  Height = 2338
  | | | | | | 6)  BitsPerComponent = 8
  | | | | | | 7)  Length = 425229
  | | | | | | 8)  ColorSpace = /DeviceGray
  | | | 1)  Rotate = 90
  | | | 2)  Parent = ref(1 0 R)
  | | | 3)  Contents (SubDirectory) -->
  | | | + [Contents directory with 1 entries]
  | | | | 0)  Length = 57
  | | | 4)  Type = /Page
  | | | 5)  MediaBox = [22,440,504,820]
  | | | 6)  CropBox = [22,440,504,820]
  | | 1)  Type = /Pages
  | | 2)  PageCount = 1
  3)  Size = 10
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

mpy*_*mpy 6

问题出现了,因为ImageMagick无法确定输入 PDF 的分辨率,因此使用默认分辨率 72x72dpi,如convert -verbose 0038.pdf out.pdf输出所示:

[ghostscript library] -q -dQUIET (...) "-r72x72" (...)
Run Code Online (Sandbox Code Playgroud)

您应用了正确的 option -density 300,但作为输出选项,即在输入文件名之后。事实上,大多数选项convert都是输出选项,但man convert知道

(...) 有限数量的设置是输入选项。它们包括:-antialias、-caption、-density、-define、-encoding、-font、-pointsize、-size 和 -texture 以及任何其他选项。

所以,正确的convert命令应该是:

convert -density 300 0038.pdf -brightness-contrast 0x10% out.pdf
Run Code Online (Sandbox Code Playgroud)

最后说两点:

  • 我会采用无损压缩,如-compress LZW;该-quality选项仅适用于JPEG/MIFF/PNG 压缩

  • -brightness-contrast可能不适用于黑白文档,并且不会影响某些 PDF 阅读器对此类文件的抗锯齿屏幕显示。(通常用于扫描的期刊文章。)