-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
60 lines (50 loc) · 1.01 KB
/
main.go
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
60
package main
import (
"fmt"
"image/jpeg"
"os"
"path"
"path/filepath"
"strings"
"github.com/gen2brain/go-fitz"
)
func main() {
var files []string
root := "pdf/"
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".pdf" {
files = append(files, path)
}
return nil
})
if err != nil {
panic(err)
}
for _, file := range files {
doc, err := fitz.New(file)
if err != nil {
panic(err)
}
folder := strings.TrimSuffix(path.Base(file), filepath.Ext(path.Base(file)))
// Extract pages as images
for n := 0; n < doc.NumPage(); n++ {
img, err := doc.Image(n)
if err != nil {
panic(err)
}
err = os.MkdirAll("img/"+folder, 0755)
if err != nil {
panic(err)
}
f, err := os.Create(filepath.Join("img/"+folder+"/", fmt.Sprintf("image-%05d.jpg", n)))
if err != nil {
panic(err)
}
err = jpeg.Encode(f, img, &jpeg.Options{Quality: jpeg.DefaultQuality})
if err != nil {
panic(err)
}
f.Close()
}
}
}