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


3

我想对pdf应用一些改进(改变亮度,对比度等),使其更具可读性,因此我选择了ImageMagick和pdftk。我使用以下命令将pdf分成几个单页pdf文件,因此我可以一次使用ImageMagick一个文件。

pdftk a.pdf burst output %04d.pdf

这时,一切都还好。我从其中一个文件(例如0038.pdf)进行测试。例如,要调整对比度,我使用此命令:

convert 0038.pdf -quality 100 -density 300 -brightness-contrast 0x10% out.pdf

但这是结果:

原版的

原文pdf

翻新

转换为pdf

我试图改变质量,密度,大小,调整大小,几何和输出的值,pdf具有不同的大小/分辨率,但总是不可读。所以我意识到问题是转换的上游。看起来输入的pdf大小和分辨率是错误的convert

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

convert -verbose 0038.pdf out.pdf

我明白了:

/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"

如您所见,尺寸为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

有任何想法吗?


1
你能添加/附加示例PDF文件吗?
ozbek

Answers:


6

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

[ghostscript library] -q -dQUIET (...) "-r72x72" (...)

您应用了正确的选项-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

最后,两个评论:

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

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.