Upgrade to Pro — share decks privately, control downloads, hide ads and more …

国立国会図書館デジタルコレクションの本をオフラインで読む

 国立国会図書館デジタルコレクションの本をオフラインで読む

Avatar for Kenichiro MATOHARA

Kenichiro MATOHARA

May 19, 2024
Tweet

More Decks by Kenichiro MATOHARA

Other Decks in Technology

Transcript

  1. 国立国会図書館デジタルコレク ションダウンロード機能 「印刷」機能で PDFファイルを生成してダウンロードできる 1度に作れるのは 50コマ迄 例えば151以上200コマの本の場合このように4回指定するこ とで4つの PDFファイルを入手できる 「1-50」「51-100」

    「101-150」「151-」 しばらく待つと「印刷用ファイルを作成しました。右のリンクから PDFファイルを表示できます。」と表示され,「PDFファイルを開く」 からダウンロード可能 11
  2. 12

  3. PDFファイル結合 pdftk(pdfchain) を利用……失敗 鍵が掛かっている poppler-utilsのpdfimages で PDFファイル内の画像書き出しは可 能 $ pdfinfo

    digidepo_2530201_0001-001-050.pdf | grep ^Enc Encrypted: yes (print:yes copy:no change:no addNotes:no algorithm:AES-256) $ pdfimages digidepo_2530201_0001-001-050.pdf -all 14
  4. 画像からPDFファイルに変換 (Imagemagick) メモリが足りず失敗, $ convert 2530201/* 宇宙船ビーグル号の冒険.pdf : convert-im6.q16: cache

    resources exhausted `2530201/2530201-107.jpg' @ error/cache.c/OpenPixel convert-im6.q16: TooManyExceptions (exception processing is suspended) @ warning/exception.c/Th convert-im6.q16: cache resources exhausted `2530201/2530201-108.jpg' @ error/cache.c/OpenPixel 15
  5. /etc/ImageMagick-6/policy.xml の resource 内の memory を 1024MiB → 8192MiB で動くように

    6GB以上必要だった.リソースを食いすぎ…… $ sudo git -C /etc diff /etc/ImageMagick-6/policy.xml (5) diff --git a/ImageMagick-6/policy.xml b/ImageMagick-6/policy.xml index 4b878ce..aa54cbe 100644 --- a/ImageMagick-6/policy.xml +++ b/ImageMagick-6/policy.xml @@ -95,7 +95,7 @@ <!-- Set maximum amount of memory in bytes to allocate for the pixel cache from the heap. When this limit is exceeded, the image pixels are cached to memory-mapped disk. --> - <policy domain="resource" name="memory" value="1024MiB"/> + <policy domain="resource" name="memory" value="8192MiB"/> <!-- Set maximum amount of memory map in bytes to allocate for the pixel cache. When this limit is exceeded, the image pixels are cached to disk. --> $ identify -list resource | grep ^\ \ Memory: Memory: 8GiB 16
  6. を利用 少リソースかつ高速 :) img2pdf https://gitlab.mister-muffin.de/josch/img2pdf $ img2pdf -o 宇宙船ビーグル号の冒険-img2pdf.pdf 2530201/*

    $ find 2530201 -type f -print0 | sort -Vz | img2pdf -o 宇宙船ビーグル号の冒険.pdf $ time convert 2530201/* 宇宙船ビーグル号の冒険.pdf real 5m18.636s user 0m47.748s sys 0m31.136s $ time img2pdf -o 宇宙船ビーグル号の冒険-img2pdf.pdf 2530201/* real 0m1.279s user 0m0.556s sys 0m0.180s 17
  7. 本の余白を削除したい 以前は ImageMagick の crop で座標を指定して切り出ししてい た 大雑把に切り抜く感じ Imagemagick の

    trim で と fuzz で余白を検知させてカットを知 ったので今回はこれを試した fuzz の % は画像により丁度いい値を探す必要がある $ convert 2530201/2530201-001.jpg -fuzz 60% -trim 2530201-001-trim.jpg 18
  8. 2つ目以降のPDFファイルも処理 1 古いデータを削除 2 PDF 画像を取り出し 3 連番部分を 50 加算したファイル名に改名

    4 余白削除 5 非圧縮で zip アーカイブに追加 $ rm 2530201/* $ pdfimages -all ./digidepo_2530201_0001-051-100.pdf 2530201/2530201 $ rename 's/(\d+)-(\d+)/sprintf "$1-%03d",$2 + 50/e' 2530201/* $ mogrify -fuzz 60% -trim 2530201/* $ find 2530201 -print | sort -V | zip -@0 ./宇宙船ビーグル号の冒険.zip 1 2 3 4 5 20
  9. (prename) でファイル名を変更 *-0.jpg → *-2.jpg に変換 *-1.jpg(右), *-2.jpg(左) になった PDFファイルにまとめる

     ざっと見るとやはりノドがずれて文が切れてしまう場所もある remane $ rename -v 's/-0\.jpg$/-2\.jpg/' * $ find . -type f -print0 | sort -Vz | img2pdf -o ../宇宙船ビーグル号の冒険-half.pdf 24
  10. 25