Created
April 11, 2012 12:25
-
-
Save arika/2359030 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# encoding: utf-8 | |
require 'mini_magick' | |
require 'origami' | |
include Origami | |
def fixup!(pdf) | |
pdf.root_objects.each do |obj| | |
next unless obj.is_a?(Graphics::ImageXObject) | |
Dir.mktmpdir do |dir| | |
ext, data = obj.to_image_file | |
tf = "#{dir}/tmp.#{ext}" | |
open(tf, 'w:binary') {|io| io.print data } | |
img = MiniMagick::Image.open(tf) | |
cs = img['colorspace'] | |
img.combine_options do |c| | |
if cs == 'DirectClassRGB' | |
c.auto_level | |
else | |
c.level '20%,80%' | |
c.sharpen '5' | |
c.colorspace 'gray' | |
end | |
c.resize 'x1400' | |
end | |
img.write(tf) | |
open(tf, 'r:binary') do |io| | |
obj.rawdata = io.read | |
end | |
obj.Width, obj.Height = img['dimensions'] | |
end | |
end | |
end | |
ARGV.each do |fn| | |
pdf = PDF.read(fn) | |
fixup!(pdf) | |
pdf.saveas("t_#{fn}") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment