-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtesseract.rb
More file actions
59 lines (49 loc) · 1.22 KB
/
Copy pathrtesseract.rb
File metadata and controls
59 lines (49 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'rtesseract'
require 'mimemagic'
require 'pry'
require "prawn"
require 'fileutils'
require 'combine_pdf'
class ImageToPDF
attr_reader :path
def initialize(path)
@path = path
create_pdf(path)
end
def create_pdf(path)
Dir.foreach(path) do |image|
if image.end_with?(".jpg")
pathname = path + '/' + image
is_it_an_image?(pathname)
analyzed_image = RTesseract.new(pathname)
pdf = analyzed_image.to_pdf
FileUtils.move pdf, './pdf/' + File.basename(image,File.extname(image)) + '.pdf'
end
end
pdf_paths = []
Dir.foreach('./pdf') do |pdf_path|
if pdf_path.end_with?('.pdf')
pdf_paths.push('./pdf/' + pdf_path)
end
end
pdf_paths.sort_by { |x| -x[/\d+/].to_i }
pdf = CombinePDF.new
pdf_paths.each do |lonely_pdf|
pdf << CombinePDF.load(lonely_pdf)
end
pdf.save('./finished_pdf/java.pdf')
end
private
def missing_image
raise 'An image is required'
end
def is_it_an_image?(image)
begin
file = File.open(image)
MimeMagic.by_magic(file).type == "image/jpeg" ? true : missing_image
rescue => error
missing_image
end
end
end
test = ImageToPDF.new('./images/java')