9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Origami で PDF に埋めこまれている画像を抽出する

9
Posted at

以下のようなコードで抽出できる。

extract_images.rb
# coding: utf-8                                                                                                                           
require 'origami'                                                                                                                         
                                                                                                                                          
include Origami                                                                                                                           
                                                                                                                                          
pdf = PDF.read(ARGV[0])                                                                                                                   
                                                                                                                                          
images = pdf.root_objects.find_all {|obj| obj.is_a?(Graphics::ImageXObject)}                                                              
                                                                                                                                          
images.each do |stream|                                                                                                                   
  ext, image_data = stream.to_image_file                                                                                                  
  image_file = "image_#{stream.reference.refno}.#{ext}"                                                                                   
  File.open(image_file, "wb") do |f|                                                                                                      
   f.write(image_data)                                                                                                                    
  end                                                                                                                                     
end           

使い方

$ ruby extract_images.rb PDFファイル名
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?