Skip to content

Instantly share code, notes, and snippets.

@timchunght
Forked from ryoco/.gitignore
Last active October 22, 2015 06:48
Show Gist options
  • Save timchunght/ee8bd4bfb8aaaab4fafc to your computer and use it in GitHub Desktop.
Save timchunght/ee8bd4bfb8aaaab4fafc to your computer and use it in GitHub Desktop.

Revisions

  1. @ryoco ryoco revised this gist Jan 4, 2015. 1 changed file with 60 additions and 58 deletions.
    118 changes: 60 additions & 58 deletions pdfToImage.go
    Original file line number Diff line number Diff line change
    @@ -1,76 +1,78 @@
    package main

    import (
    "fmt"
    "strings"
    "os"
    "log"
    "flag"
    "io/ioutil"
    "github.com/gographics/imagick/imagick"
    "flag"
    "fmt"
    "github.com/gographics/imagick/imagick"
    "io/ioutil"
    "log"
    "os"
    "strings"
    )

    func main() {
    flag.Parse()
    args := flag.Args()
    // set args
    flag.Parse()
    args := flag.Args()

    readdir, writedir := getRWdir(args)
    flists := getPdfLists(readdir)
    os.Mkdir(writedir, 0777)
    // set directories
    readdir, writedir := getRWdir(args)
    pdfFiles := getPdfLists(readdir)
    os.Mkdir(writedir, 0777)

    for _, filename := range flists {
    fmt.Println(filename)
    filename := filename + "[0]"
    imgname := writedir + "/" + strings.Replace(filename, "pdf[0]", "png", 1)
    if _, err := os.Stat(imgname); os.IsNotExist(err) {
    fmt.Println("processing...")
    convert(filename, imgname)
    }
    }
    // convert pdf to img
    for _, filename := range pdfFiles {
    fmt.Println(filename)
    filename := filename + "[0]"
    imgname := writedir + "/" + strings.Replace(filename, "pdf[0]", "png", 1)
    if _, err := os.Stat(imgname); os.IsNotExist(err) {
    fmt.Println("processing...")
    convertPdfToImg(filename, imgname)
    }
    }
    }

    func getRWdir(args []string) (string, string) {
    r := "./"
    w := "./img"
    if len(args) > 0 {
    r = args[0]
    }
    if len(args) > 1 {
    w = args[1]
    }
    return r, w
    r := "./"
    w := "./img"
    if len(args) > 0 {
    r = args[0]
    }
    if len(args) > 1 {
    w = args[1]
    }
    return r, w
    }

    func getPdfLists(readdir string) []string {
    files, _ := ioutil.ReadDir(readdir)
    filenames := []string{}
    for _, f := range files {
    filename := f.Name()
    if (strings.HasSuffix(filename, ".pdf")) {
    filenames = append(filenames, filename)
    }
    }
    return filenames
    files, _ := ioutil.ReadDir(readdir)
    filenames := []string{}
    for _, f := range files {
    filename := f.Name()
    if strings.HasSuffix(filename, ".pdf") {
    filenames = append(filenames, filename)
    }
    }
    return filenames
    }

    func convertPdfToImg(filename string, imgname string) {
    imagick.Initialize()
    defer imagick.Terminate()

    func convert(filename string, imgname string) {
    imagick.Initialize()
    defer imagick.Terminate()

    mw := imagick.NewMagickWand()
    re := mw.ReadImage(filename)
    if re != nil {
    log.Fatal(re)
    }
    defer mw.Destroy()
    mw := imagick.NewMagickWand()
    re := mw.ReadImage(filename)
    if re != nil {
    log.Fatal(re)
    }
    defer mw.Destroy()

    mwc := mw.Clone()
    mwc := mw.Clone()

    // mwc.AdaptiveResizeImage(30, 30)
    fmt.Println("png file created: " + imgname)
    w := mwc.WriteImage(imgname)
    if w != nil {
    log.Fatal(w)
    }
    // mwc.AdaptiveResizeImage(30, 30)
    fmt.Println("png file created: " + imgname)
    w := mwc.WriteImage(imgname)
    if w != nil {
    log.Fatal(w)
    }
    }
  2. @ryoco ryoco revised this gist Jan 3, 2015. 2 changed files with 67 additions and 31 deletions.
    2 changes: 1 addition & 1 deletion .gitignore
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    /.swp
    /*.swp
    /*.pdf
    /*.png
    .DS_Store
    96 changes: 66 additions & 30 deletions pdfToImage.go
    Original file line number Diff line number Diff line change
    @@ -1,40 +1,76 @@
    package main

    import "fmt"
    import "strings"
    import "os"
    import "log"
    import "flag"
    import "github.com/gographics/imagick/imagick"
    import (
    "fmt"
    "strings"
    "os"
    "log"
    "flag"
    "io/ioutil"
    "github.com/gographics/imagick/imagick"
    )

    func main() {
    flag.Parse()
    args := flag.Args()
    fmt.Println(args[0])

    filename := args[0] + "[0]" //"sample.pdf[0]"
    imgname := strings.Replace(filename, "pdf[0]", "", 1) + "png"
    if _, err := os.Stat(imgname); os.IsNotExist(err) {
    fmt.Println("processing...")
    imagick.Initialize()
    defer imagick.Terminate()

    mw := imagick.NewMagickWand()
    re := mw.ReadImage(filename)
    if re != nil {
    log.Fatal(re)

    readdir, writedir := getRWdir(args)
    flists := getPdfLists(readdir)
    os.Mkdir(writedir, 0777)

    for _, filename := range flists {
    fmt.Println(filename)
    filename := filename + "[0]"
    imgname := writedir + "/" + strings.Replace(filename, "pdf[0]", "png", 1)
    if _, err := os.Stat(imgname); os.IsNotExist(err) {
    fmt.Println("processing...")
    convert(filename, imgname)
    }
    defer mw.Destroy()

    mwc := mw.Clone()

    // mwc.AdaptiveResizeImage(30, 30)
    fmt.Println("png file created: " + imgname)
    w := mwc.WriteImage(imgname)
    if w != nil {
    log.Fatal(w)
    }
    }

    func getRWdir(args []string) (string, string) {
    r := "./"
    w := "./img"
    if len(args) > 0 {
    r = args[0]
    }
    if len(args) > 1 {
    w = args[1]
    }
    return r, w
    }

    func getPdfLists(readdir string) []string {
    files, _ := ioutil.ReadDir(readdir)
    filenames := []string{}
    for _, f := range files {
    filename := f.Name()
    if (strings.HasSuffix(filename, ".pdf")) {
    filenames = append(filenames, filename)
    }
    } else {
    fmt.Println("img file already exists")
    }
    return filenames
    }


    func convert(filename string, imgname string) {
    imagick.Initialize()
    defer imagick.Terminate()

    mw := imagick.NewMagickWand()
    re := mw.ReadImage(filename)
    if re != nil {
    log.Fatal(re)
    }
    defer mw.Destroy()

    mwc := mw.Clone()

    // mwc.AdaptiveResizeImage(30, 30)
    fmt.Println("png file created: " + imgname)
    w := mwc.WriteImage(imgname)
    if w != nil {
    log.Fatal(w)
    }
    }
  3. @ryoco ryoco revised this gist Jan 2, 2015. 2 changed files with 30 additions and 27 deletions.
    3 changes: 3 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -1 +1,4 @@
    /.swp
    /*.pdf
    /*.png
    .DS_Store
    54 changes: 27 additions & 27 deletions pdfToImage.go
    Original file line number Diff line number Diff line change
    @@ -8,33 +8,33 @@ import "flag"
    import "github.com/gographics/imagick/imagick"

    func main() {
    flag.Parse()
    args := flag.Args()
    fmt.Println(args[0])
    flag.Parse()
    args := flag.Args()
    fmt.Println(args[0])

    filename := args[0] + "[0]" //"sample.pdf[0]"
    imgname := strings.Replace(filename, "pdf[0]", "", 1) + "png"
    if _, err := os.Stat(imgname); os.IsNotExist(err) {
    fmt.Println("processing...")
    imagick.Initialize()
    defer imagick.Terminate()

    filename := args[0] + "[0]" //"sample.pdf[0]"
    imgname := strings.Replace(filename, "pdf[0]", "", 1) + "png"
    if _, err := os.Stat(imgname); os.IsNotExist(err) {
    fmt.Println("processing...")
    imagick.Initialize()
    defer imagick.Terminate()

    mw := imagick.NewMagickWand()
    re := mw.ReadImage(filename)
    if re != nil {
    log.Fatal(re)
    }
    defer mw.Destroy()

    mwc := mw.Clone()

    // mwc.AdaptiveResizeImage(30, 30)
    fmt.Println("png file created: " + imgname)
    w := mwc.WriteImage(imgname)
    if w != nil {
    log.Fatal(w)
    }
    } else {
    fmt.Println("img file already exists")
    mw := imagick.NewMagickWand()
    re := mw.ReadImage(filename)
    if re != nil {
    log.Fatal(re)
    }
    defer mw.Destroy()

    mwc := mw.Clone()

    // mwc.AdaptiveResizeImage(30, 30)
    fmt.Println("png file created: " + imgname)
    w := mwc.WriteImage(imgname)
    if w != nil {
    log.Fatal(w)
    }
    } else {
    fmt.Println("img file already exists")
    }
    }
  4. @ryoco ryoco revised this gist Jan 2, 2015. 4 changed files with 41 additions and 27 deletions.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    /.swp
    File renamed without changes.
    27 changes: 0 additions & 27 deletions pdfToImage
    Original file line number Diff line number Diff line change
    @@ -1,27 +0,0 @@
    package main

    import "log"
    import "github.com/gographics/imagick/imagick"

    func main() {

    filename := "sample.pdf[0]"
    imagick.Initialize()
    defer imagick.Terminate()

    mw := imagick.NewMagickWand()
    re := mw.ReadImage(filename)
    if re != nil {
    log.Fatal(re)
    }
    defer mw.Destroy()

    mwc := mw.Clone()

    // mwc.AdaptiveResizeImage(30, 30)
    w := mwc.WriteImage("pdfimg.png")
    if w != nil {
    log.Fatal(w)
    }

    }
    40 changes: 40 additions & 0 deletions pdfToImage.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    package main

    import "fmt"
    import "strings"
    import "os"
    import "log"
    import "flag"
    import "github.com/gographics/imagick/imagick"

    func main() {
    flag.Parse()
    args := flag.Args()
    fmt.Println(args[0])

    filename := args[0] + "[0]" //"sample.pdf[0]"
    imgname := strings.Replace(filename, "pdf[0]", "", 1) + "png"
    if _, err := os.Stat(imgname); os.IsNotExist(err) {
    fmt.Println("processing...")
    imagick.Initialize()
    defer imagick.Terminate()

    mw := imagick.NewMagickWand()
    re := mw.ReadImage(filename)
    if re != nil {
    log.Fatal(re)
    }
    defer mw.Destroy()

    mwc := mw.Clone()

    // mwc.AdaptiveResizeImage(30, 30)
    fmt.Println("png file created: " + imgname)
    w := mwc.WriteImage(imgname)
    if w != nil {
    log.Fatal(w)
    }
    } else {
    fmt.Println("img file already exists")
    }
    }
  5. @ryoco ryoco created this gist Oct 30, 2014.
    2 changes: 2 additions & 0 deletions brew
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    brew install imagemagick
    brew install ghostscript
    27 changes: 27 additions & 0 deletions pdfToImage
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    package main

    import "log"
    import "github.com/gographics/imagick/imagick"

    func main() {

    filename := "sample.pdf[0]"
    imagick.Initialize()
    defer imagick.Terminate()

    mw := imagick.NewMagickWand()
    re := mw.ReadImage(filename)
    if re != nil {
    log.Fatal(re)
    }
    defer mw.Destroy()

    mwc := mw.Clone()

    // mwc.AdaptiveResizeImage(30, 30)
    w := mwc.WriteImage("pdfimg.png")
    if w != nil {
    log.Fatal(w)
    }

    }