imagemagick 是一个功能丰富的图片处理工具
具体安装方式可以参考官方,macos 上可以通过 homebrew 安装
brew install imagemagick@6
homebrew 最新的源是 7.* 版本,由于我的场景需要在 linux 部署,linux 的 apt 源目前是 6.9, 为了保持一致,所以使用的是旧版本
命令行使用
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
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 }
特别地,需要设置两个环境变量
export cgo_cflags_allow='-xpreprocessor' export pkg_config_path="/usr/local/opt/imagemagick@6/lib/pkgconfig" # 取决于 brew install 的输出
golang pdf转jpeg
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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!