網站首頁 編程語言 正文
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) }
原文鏈接:https://blog.csdn.net/oqqYuan1234567890/article/details/127197307
相關推薦
- 2022-07-30 Linux secure 日志分析
- 2022-06-12 Python基于socket實現TCP客戶端和服務端_python
- 2022-12-31 Mobx實現React?應用的狀態管理詳解_React
- 2023-02-10 python自定義函數中的return和print使用及說明_python
- 2022-11-03 C++智能指針shared_ptr與weak_ptr的實現分析_C 語言
- 2022-05-22 Nginx圖片服務器配置之后圖片訪問404的問題解決_nginx
- 2022-04-11 following signatures couldn‘t be verified because
- 2022-07-24 Python使用apscheduler模塊設置定時任務的實現_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支