-
-
Save torreypayne/6a94177fe40a1628b5ab3ff16a85778f to your computer and use it in GitHub Desktop.
OCR Image-based PDF in ruby
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
require 'parallel' | |
require 'rtesseract' | |
require 'mini_magick' | |
source = "/MyDirectory/my.pdf" | |
doc = {} | |
pdf = MiniMagick::Image.open(source) | |
Parallel.map(pdf.pages.each_with_index, in_threads: 8) do |page, idx| | |
tmpfile = Tempfile.new(['', '.tif']) | |
MiniMagick::Tool::Convert.new do |convert| | |
convert.density(300) | |
convert << page.path | |
convert.alpha("off") | |
convert << tmpfile.path | |
end | |
tess = RTesseract.new(tmpfile.path) | |
doc[idx] = tess.to_s | |
tmpfile.unlink | |
end | |
doc.sort.to_h.values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment