ImageMagick It is a feature-rich image processing tool
For specific installation methods, please refer to the official. You can install them through homebrew on MacOS.
brew install imagemagick@6
The latest source of homebrew is version 7.*. Since my scenario needs to be deployed on Linux, the apt source of linux is currently 6.9. In order to be consistent, the old version is used.
Command line usage
convert -density 128 -quality 100 -alpha remove
Golang code usage
Core points:
pdf needs to remove the alpha channel, and then set the background color white (you can set other colors according to your needs) to pay attention to memory leakage, because this is cgo, and once it is leaked, it will be gg. For example, you don't()
The above density setting is resolution. You need to set a reasonable value, otherwise the converted picture will be blurred.
The binding installation method of golang can be as follows github introduction/gographics/imagick
package main import ( "fmt" "io/ioutil" "runtime" "runtime/debug" "time" "/gographics/imagick.v2/imagick" ) func main() { () //defer () data, _ := ("") start := () for i := 0; i < 100; i++ { if i%10 == 0 { ("i", i) } go createCoverImage(data, "") } ("duration", ().Sub(start)) PrintMemUsage() () PrintMemUsage() (10 * ) () ("free cgo") PrintMemUsage() (10 * ) } // PrintMemUsage outputs the current, total and OS memory being used. As well as the number // of garage collection cycles completed. func PrintMemUsage() { var m (&m) // For info on each, see: /pkg/runtime/#MemStats ("Alloc = %v MiB", bToMb()) ("\tTotalAlloc = %v MiB", bToMb()) ("\tSys = %v MiB", bToMb()) ("\tNumGC = %v\n", ) } func bToMb(b uint64) uint64 { return b / 1024 / 1024 } func clearImagickWand(mw *) { () () () //(mw, nil) mw = nil } func createCoverImage(data []byte, coverPathName string) bool { //sourceImagePath := getSourceImageForCover((pathNoExtension)) mw := () defer clearImagickWand(mw) (192, 192) err := (data) if err != nil { return false } //length := () //("length", length) //("width", ()) //("height", ()) pix := () ("white") //(pix) (imagick.ALPHA_CHANNEL_REMOVE) ("jpeg") err = (coverPathName) if err != nil { return false } _ = () return true }
In particular, two environment variables need to be set
export CGO_CFLAGS_ALLOW='-Xpreprocessor' export PKG_CONFIG_PATH="/usr/local/opt/imagemagick@6/lib/pkgconfig" # Depends on the output of brew install
Golang PDF to JPEG
package main import ( "fmt" "os" "/h2non/bimg" ) func main() { buffer, err := ("") if err != nil { (, err) } newImage, err := (buffer).Convert() if err != nil { (, err) } if (newImage).Type() == "jpeg" { (, "The image was converted into jpeg") } ("", newImage) }
This is the article about golang to realize pdf to high definition jpeg. This is all about this article. For more related golang pdf to high definition jpeg content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!