golang?實(shí)現(xiàn)?pdf?轉(zhuǎn)高清晰度?jpeg的處理方法
ImageMagick 是一個(gè)功能豐富的圖片處理工具
具體安裝方式可以參考官方,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 代碼使用
核心要點(diǎn):
pdf 需要去除 alpha 通道,然后背景色設(shè)置白色(你可以可以根據(jù)需求設(shè)置其它顏色)留意內(nèi)存泄露,因?yàn)檫@是 cgo,一旦泄露就 gg 了。比如你沒有 mw.RemoveImage()
上述的 density 設(shè)置就是 resolution, 需要設(shè)置一個(gè)合理的值,否則轉(zhuǎn)換的圖片就會(huì)糊
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è)置兩個(gè)環(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 實(shí)現(xiàn) pdf 轉(zhuǎn)高清晰度 jpeg的文章就介紹到這了,更多相關(guān)golang pdf 轉(zhuǎn)高清晰度 jpeg內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Golang快速構(gòu)建出命令行應(yīng)用程序
在日常開發(fā)中,大家對(duì)命令行工具(CLI)想必特別熟悉了,如果說你不知道命令工具,那你可能是個(gè)假開發(fā)。每天都會(huì)使用大量的命令行工具,例如最常用的Git、Go、Docker等,這篇文章主要介紹了使用Golang快速構(gòu)建出命令行應(yīng)用程序,需要的朋友可以參考下2023-02-02解決Golang map range遍歷結(jié)果不穩(wěn)定問題
這篇文章主要介紹了解決Golang map range遍歷結(jié)果不穩(wěn)定問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12idea搭建go環(huán)境實(shí)現(xiàn)go語言開發(fā)
這篇文章主要給大家介紹了關(guān)于idea搭建go環(huán)境實(shí)現(xiàn)go語言開發(fā)的相關(guān)資料,文中通過圖文介紹以及代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用go具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01文字解說Golang Goroutine和線程的區(qū)別
goroutine 是 Go語言中的輕量級(jí)線程實(shí)現(xiàn),由 Go 運(yùn)行時(shí)(runtime)管理,使用每一個(gè) go 關(guān)鍵字將會(huì)額外開啟一個(gè)新的協(xié)程 goroutine,今天通過本文給大家介紹下Golang Goroutine和線程的區(qū)別,感興趣的朋友一起看看吧2022-03-03Golang unsafe.Sizeof函數(shù)代碼示例使用解析
這篇文章主要為大家介紹了Golang unsafe.Sizeof函數(shù)代碼示例使用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Go語言中init函數(shù)和defer延遲調(diào)用關(guān)鍵詞詳解
這篇文章主要介紹了Go語言中init函數(shù)和defer延遲調(diào)用關(guān)鍵詞,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03詳解Go語言如何使用標(biāo)準(zhǔn)庫sort對(duì)切片進(jìn)行排序
Sort?標(biāo)準(zhǔn)庫提供了對(duì)基本數(shù)據(jù)類型的切片和自定義類型的切片進(jìn)行排序的函數(shù)。今天主要分享的內(nèi)容是使用?Go?標(biāo)準(zhǔn)庫?sort?對(duì)切片進(jìn)行排序,感興趣的可以了解一下2022-12-12