Skip to content

Commit 5401095

Browse files
committed
Issue #16, customizable graphicsmagick --density when extracting images.
1 parent 6552b45 commit 5401095

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

index.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,15 @@ <h2 id="usage">Usage</h2>
177177
</p>
178178

179179
<p>
180-
<b class="header">images</b><code>--size --format --pages </code>
180+
<b class="header">images</b><code>--size --format --pages --density</code>
181181
<span class="alias">Ruby: <b>extract_images</b></span>
182182
<br />
183183
Generates an image for each page in the document at the specified resolution
184184
and format. Pass <tt>--pages</tt> or <tt>-p</tt> to choose the specific pages to
185185
image. Passing<br /> <tt>--size</tt> or <tt>-s</tt> will specify the desired
186-
image resolution, and <tt>--format</tt> or <tt>-f</tt> will select the format of the final images.
186+
image resolution, <tt>--density</tt> or <tt>-d</tt> will specify the DPI to rasterize the images
187+
at during conversion by GraphicsMagick, and <tt>--format</tt> or <tt>-f</tt>
188+
will select the format of the final images.
187189
</p>
188190
<pre>
189191
docsplit images example.pdf

lib/docsplit/command_line.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ def parse_options
8585
opts.on('-f', '--format [FORMAT]', 'set image format (pdf, jpg, gif...)') do |t|
8686
@options[:format] = t.split(',')
8787
end
88+
opts.on('-d', '--density [NUM]', 'set image density (DPI) when rasterizing') do |d|
89+
@options[:density] = d
90+
end
8891
opts.on('--[no-]ocr', 'force OCR to be used, or disable OCR') do |o|
8992
@options[:ocr] = o
9093
end

lib/docsplit/image_extractor.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module Docsplit
44
# nicely sized images.
55
class ImageExtractor
66

7-
DENSITY_ARG = "-density 150"
87
MEMORY_ARGS = "-limit memory 256MiB -limit map 512MiB"
98
DEFAULT_FORMAT = :png
9+
DEFAULT_DENSITY = '150'
1010

1111
# Extract a list of PDFs as rasterized page images, according to the
1212
# configuration in options.
@@ -34,7 +34,7 @@ def convert(pdf, size, format, previous=nil)
3434
pages = @pages || '1-' + Docsplit.extract_length(pdf).to_s
3535
escaped_pdf = ESCAPE[pdf]
3636
FileUtils.mkdir_p(directory) unless File.exists?(directory)
37-
common = "#{MEMORY_ARGS} #{DENSITY_ARG} #{resize_arg(size)} #{quality_arg(format)}"
37+
common = "#{MEMORY_ARGS} -density #{@density} #{resize_arg(size)} #{quality_arg(format)}"
3838
if previous
3939
FileUtils.cp(Dir[directory_for(previous) + '/*'], directory)
4040
result = `MAGICK_TMPDIR=#{tempdir} OMP_NUM_THREADS=2 gm mogrify #{common} -unsharp 0x0.5+0.75 \"#{directory}/*.#{format}\" 2>&1`.chomp
@@ -58,6 +58,7 @@ def convert(pdf, size, format, previous=nil)
5858
def extract_options(options)
5959
@output = options[:output] || '.'
6060
@pages = options[:pages]
61+
@density = options[:density] || DEFAULT_DENSITY
6162
@formats = [options[:format] || DEFAULT_FORMAT].flatten
6263
@sizes = [options[:size]].flatten.compact
6364
@sizes = [nil] if @sizes.empty?

0 commit comments

Comments
 (0)