脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - golang 实现 pdf 转高清晰度 jpeg的处理方法

golang 实现 pdf 转高清晰度 jpeg的处理方法

2022-11-25 11:58皿小草 Golang

这篇文章主要介绍了golang 实现 pdf 转高清晰度 jpeg,下面主要介绍Golang 代码使用方法及Golang PDF转JPEG的详细代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

ImageMagick 是一个功能丰富的图片处理工具

具体安装方式可以参考官方,MacOS 上可以通过 homebrew 安装

?
1
brew install imagemagick@6

homebrew 最新的源是 7.* 版本,由于我的场景需要在 linux 部署,linux 的 apt 源目前是 6.9, 为了保持一致,所以使用的是旧版本

命令行使用

?
1
convert -density 128 1.pdf -quality 100 -alpha remove output.jpeg

Golang 代码使用

核心要点:

pdf 需要去除 alpha 通道,然后背景色设置白色(你可以可以根据需求设置其它颜色)留意内存泄露,因为这是 cgo,一旦泄露就 gg 了。比如你没有 mw.RemoveImage()上述的 density 设置就是 resolution, 需要设置一个合理的值,否则转换的图片就会糊

golang 的 binding 安装方式可以按照 github 介绍 https://github.com/gographics/imagick

?
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
 
import (
    "fmt"
    "io/ioutil"
    "runtime"
    "runtime/debug"
    "time"
 
    "gopkg.in/gographics/imagick.v2/imagick"
)
 
func main() {
    imagick.Initialize()
    //defer imagick.Terminate()
    data, _ := ioutil.ReadFile("1.pdf")
 
    start := time.Now()
    for i := 0; i < 100; i++ {
        if i%10 == 0 {
            fmt.Println("i", i)
        }
        go createCoverImage(data, "1-1.jpeg")
    }
    fmt.Println("duration", time.Now().Sub(start))
    PrintMemUsage()
    debug.FreeOSMemory()
    PrintMemUsage()
    time.Sleep(10 * time.Second)
    imagick.Terminate()
    fmt.Println("free cgo")
    PrintMemUsage()
    time.Sleep(10 * time.Minute)
}
 
// PrintMemUsage outputs the current, total and OS memory being used. As well as the number
// of garage collection cycles completed.
func PrintMemUsage() {
    var m runtime.MemStats
    runtime.ReadMemStats(&m)
    // For info on each, see: https://golang.org/pkg/runtime/#MemStats
    fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
    fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
    fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
    fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
 
func bToMb(b uint64) uint64 {
    return b / 1024 / 1024
}
 
func clearImagickWand(mw *imagick.MagickWand) {
    mw.RemoveImage()
    mw.Clear()
    mw.Destroy()
    //runtime.SetFinalizer(mw, nil)
    mw = nil
}
 
func createCoverImage(data []byte, coverPathName string) bool {
    //sourceImagePath := getSourceImageForCover(filepath.Dir(pathNoExtension))
    mw := imagick.NewMagickWand()
    defer clearImagickWand(mw)
    mw.SetResolution(192, 192)
    err := mw.ReadImageBlob(data)
    if err != nil {
        return false
    }
 
    //length := mw.GetImageIterations()
    //fmt.Println("length", length)
    //fmt.Println("width", mw.GetImageWidth())
    //fmt.Println("height", mw.GetImageHeight())
 
    pix := imagick.NewPixelWand()
    pix.SetColor("white")
    //mw.SetBackgroundColor(pix)
    mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_REMOVE)
    mw.SetImageFormat("jpeg")
 
    err = mw.WriteImage(coverPathName)
    if err != nil {
        return false
    }
    _ = mw.GetImageBlob()
 
    return true
}

特别地,需要设置两个环境变量

?
1
2
export CGO_CFLAGS_ALLOW='-Xpreprocessor'
export PKG_CONFIG_PATH="/usr/local/opt/imagemagick@6/lib/pkgconfig" # 取决于 brew install 的输出

Golang PDF转JPEG

?
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
package main
 
import (
    "fmt"
    "os"
 
    "github.com/h2non/bimg"
)
 
func main() {
    buffer, err := bimg.Read("test.pdf")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
 
    newImage, err := bimg.NewImage(buffer).Convert(bimg.JPEG)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }
 
    if bimg.NewImage(newImage).Type() == "jpeg" {
        fmt.Fprintln(os.Stderr, "The image was converted into jpeg")
    }
 
    bimg.Write("test.jpg", newImage)
}

到此这篇关于golang 实现 pdf 转高清晰度 jpeg的文章就介绍到这了,更多相关golang pdf 转高清晰度 jpeg内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/oqqYuan1234567890/article/details/127197307

延伸 · 阅读

精彩推荐
  • Golanggo并发编程sync.Cond使用场景及实现原理

    go并发编程sync.Cond使用场景及实现原理

    这篇文章主要为大家介绍了go并发编程sync.Cond使用场景及实现原理详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加...

    Koffee7252022-08-31
  • Golang一文读懂go中semaphore(信号量)源码

    一文读懂go中semaphore(信号量)源码

    这篇文章主要介绍了一文读懂go中semaphore(信号量)源码的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友...

    Rick.lz6772021-04-23
  • GolangGo语法糖之‘...’ 的使用实例详解

    Go语法糖之‘...’ 的使用实例详解

    语法糖(Syntactic sugar),也译为糖衣语法,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。这篇文章主要给...

    mrr3732020-05-20
  • Golang玩转Go命令行工具Cobra

    玩转Go命令行工具Cobra

    这篇文章主要介绍了玩转Go命令行工具Cobra,本文介绍了Cobra的最基本也是最常用的使用部分,但是Cobra仍然有很多优秀的操作值得我们学习,需要的朋友可以...

    Barry Yan6842022-08-15
  • Golang安装Sublime Text支持Go插件的方法步骤

    安装Sublime Text支持Go插件的方法步骤

    本文主要介绍了安装Sublime Text支持Go插件的方法步骤,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    wangxinyu20116112022-08-28
  • GolangGolang中基础的命令行模块urfave/cli的用法说明

    Golang中基础的命令行模块urfave/cli的用法说明

    这篇文章主要介绍了Golang中基础的命令行模块urfave/cli的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    龙舞飞扬13282021-03-05
  • Golanggo语言使用Chromedp实现二维码登陆教程示例源码

    go语言使用Chromedp实现二维码登陆教程示例源码

    这篇文章主要为大家介绍了go语言使用Chromedp实现二维码登陆示例源码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    EvaCcino11352022-09-29
  • Golanggo语言中range用法

    go语言中range用法

    这篇文章主要介绍了go语言中range用法,实例分析了Go语言中range的功能及使用技巧,需要的朋友可以参考下 ...

    李大宝lidabao2862020-04-20