ImageMagick: неправильный размер при конвертации PDF в JPG

1851
supergiox

Я хочу применить некоторые улучшения (изменение яркости, контрастности и т. Д.) К 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.

Infact, когда я просто набираю эту команду:

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 

Есть идеи?

3
Можете ли вы добавить / приложить пример PDF-файла (ов)? ozbek 9 лет назад 1

1 ответ на вопрос

5
mpy

The problem arises, because ImageMagick cannot determine the resolution of the input PDF and hence is using the default resolution of 72x72dpi as shown in the convert -verbose 0038.pdf out.pdf output:

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

You applied the correct option -density 300, but as an output option, i.e. after the input file name. Indeed most options for convert are output options, but man convert knows that

(...) a limited number of setting are input-option. They include: -antialias, -caption, -density, -define, -encoding, -font, -pointsize, -size, and -texture as well as any of the miscellaneous options.

So, the correct convert command should be:

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

Finally, two remarks:

  • I would employ a lossless compression like -compress LZW; the -quality option is for JPEG/MIFF/PNG compression only.

  • -brightness-contrast probably won't work on b/w documents and does not affect the anti aliased screen display of some PDF readers for that kind of files. (Typically for scanned journal articles.)

Это сработало отлично. Спасибо вам большое! supergiox 9 лет назад 0

Похожие вопросы