Created
March 2, 2011 15:23
-
-
Save ukstudio/851085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # coding: utf-8 | |
| require 'rubygems' | |
| require 'optparse' | |
| require 'progressbar' | |
| class Progress | |
| def self.start(title, size) | |
| progress = ProgressBar.new(title, size) | |
| yield progress | |
| progress.finish | |
| end | |
| end | |
| class ImageConverter | |
| def initialize(images) | |
| @images = images | |
| @options = [] | |
| @options << "-level '0%,100%,0.1'" | |
| @options << "-strip" | |
| end | |
| def option=(option) | |
| @option = option | |
| if option[:left] && option[:top] | |
| @options << "-chop #{option[:left]}x#{option[:top]} -flip -flop" | |
| end | |
| if option[:right] && option[:bottom] | |
| @options << "-chop #{option[:right]}x#{option[:bottom]} -flip -flop" | |
| end | |
| end | |
| def convert! | |
| converted_images = [] | |
| Progress.start('convert image', @images.size) do |pr| | |
| @images.each do |image| | |
| system("convert #{@options.join(' ')} #{image} convert_#{image}") | |
| converted_images << "convert_#{image}" | |
| pr.inc | |
| end | |
| end | |
| if @option[:cover] # 表示はchopしたくない | |
| system("rm convert_#{@option[:cover]}") | |
| system("convert #{@option[:cover]} convert_#{@option[:cover]}") | |
| end | |
| return converted_images | |
| end | |
| end | |
| class ImageToPdf | |
| def initialize(images) | |
| @images = images | |
| @options = [] | |
| end | |
| def option=(option) | |
| if option[:resize] | |
| @options << "-define jpeg:size=#{option[:resize]}" | |
| @options << "-resize #{option[:resize]}" | |
| end | |
| end | |
| def convert! | |
| Progress.start('convert image to pdf', @images.size + 1) do |pr| | |
| @images.each do |image| | |
| system("convert #{@options.join(' ')} #{image} #{image.split('.')[0]}.pdf") | |
| pr.inc | |
| end | |
| system('pdftk *.pdf cat output output.pdf') | |
| pr.inc | |
| end | |
| cleanup! | |
| end | |
| def cleanup! | |
| Progress.start('cleanup', 2) do |pr| | |
| system("rm #{@images.join(' ')}") | |
| pr.inc | |
| system("rm #{@images.map{|f| f.split('.')[0] + '.pdf'}.join(' ')}") | |
| pr.inc | |
| end | |
| end | |
| end | |
| def set_option | |
| config = {} | |
| opts = OptionParser.new | |
| opts.on('-c VAL') {|v| config[:cover] = v} | |
| opts.on('-s VAL') {|v| config[:resize] = v} | |
| opts.on('-t vAL') {|v| config[:top] = v } | |
| opts.on('-b vAL') {|v| config[:bottom] = v } | |
| opts.on('-l vAL') {|v| config[:left] = v } | |
| opts.on('-r vAL') {|v| config[:right] = v } | |
| opts.parse!(ARGV) | |
| return config | |
| end | |
| config = set_option | |
| files = ARGV | |
| converter = ImageConverter.new(files) | |
| converter.option = config | |
| converted_images = converter.convert! | |
| img2pdf = ImageToPdf.new(converted_images) | |
| img2pdf.option = config | |
| img2pdf.convert! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment