golang?實現(xiàn)?pdf?轉(zhuǎn)高清晰度?jpeg的處理方法
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 通道,然后背景色設(shè)置白色(你可以可以根據(jù)需求設(shè)置其它顏色)留意內(nèi)存泄露,因為這是 cgo,一旦泄露就 gg 了。比如你沒有 mw.RemoveImage()
上述的 density 設(shè)置就是 resolution, 需要設(shè)置一個合理的值,否則轉(zhuǎn)換的圖片就會糊
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 }
特別地,需要設(shè)置兩個環(huán)境變量
export CGO_CFLAGS_ALLOW='-Xpreprocessor' export PKG_CONFIG_PATH="/usr/local/opt/imagemagick@6/lib/pkgconfig" # 取決于 brew install 的輸出
Golang PDF轉(zhuǎn)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) }
到此這篇關(guān)于golang 實現(xiàn) pdf 轉(zhuǎn)高清晰度 jpeg的文章就介紹到這了,更多相關(guān)golang pdf 轉(zhuǎn)高清晰度 jpeg內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Golang map range遍歷結(jié)果不穩(wěn)定問題
這篇文章主要介紹了解決Golang map range遍歷結(jié)果不穩(wěn)定問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12idea搭建go環(huán)境實現(xiàn)go語言開發(fā)
這篇文章主要給大家介紹了關(guān)于idea搭建go環(huán)境實現(xiàn)go語言開發(fā)的相關(guān)資料,文中通過圖文介紹以及代碼介紹的非常詳細,對大家學習或者使用go具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01文字解說Golang Goroutine和線程的區(qū)別
goroutine 是 Go語言中的輕量級線程實現(xiàn),由 Go 運行時(runtime)管理,使用每一個 go 關(guān)鍵字將會額外開啟一個新的協(xié)程 goroutine,今天通過本文給大家介紹下Golang Goroutine和線程的區(qū)別,感興趣的朋友一起看看吧2022-03-03Golang unsafe.Sizeof函數(shù)代碼示例使用解析
這篇文章主要為大家介紹了Golang unsafe.Sizeof函數(shù)代碼示例使用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12Go語言中init函數(shù)和defer延遲調(diào)用關(guān)鍵詞詳解
這篇文章主要介紹了Go語言中init函數(shù)和defer延遲調(diào)用關(guān)鍵詞,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03