Golang壓縮Jpeg圖片和PNG圖片的操作
博主一直在維護(hù)一個導(dǎo)出PDF的服務(wù),但是這個服務(wù)導(dǎo)出的PDF文件是真的巨大,動輒就上百MB。這里面主要是圖片占據(jù)了大多數(shù)體積,所以考慮在導(dǎo)出前壓縮一下圖片。
Jpeg的圖片壓縮是很好做的,因為jpeg這個協(xié)議本身就支持調(diào)整圖片質(zhì)量的。在golang中,我們只需要使用標(biāo)準(zhǔn)庫的image/jpeg,將圖片從二進(jìn)制數(shù)據(jù)解碼后,降低質(zhì)量再編碼為二進(jìn)制數(shù)據(jù)即可實現(xiàn)壓縮。而且質(zhì)量和壓縮比例相對而言還不錯。
func compressImageResource(data []byte) []byte { img, _, err := image.Decode(bytes.NewReader(data)) if err != nil { return data } buf := bytes.Buffer{} err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: 40}) if err != nil { return data } if buf.Len() > len(data) { return data } return buf.Bytes() }
比較麻煩的是壓縮PNG圖片,在網(wǎng)上找了很多相關(guān)的庫,感覺都沒什么即可以保持質(zhì)量,又可以盡可能壓縮的辦法。
//下面這兩個庫都比較偏重于轉(zhuǎn)換圖片大小,在保持寬高不變的情況下,壓縮比例很一般 https://github.com/discord/lilliput //這個庫是一家海外公司基于C語言的一個開源圖片處理庫,但是封裝的很好,不需要安裝額外依賴 https://github.com/disintegration/imaging //下面這個庫可以對PNG圖片進(jìn)行較大的壓縮,可惜壓縮比例過大時會嚴(yán)重失真 https://github.com/foobaz/lossypng/
后來,借鑒一篇博客的做法,還是先把PNG圖片轉(zhuǎn)換為Jpeg圖片,然后再將jpeg圖片的質(zhì)量降低。相對上邊這些庫,壓縮比例和質(zhì)量都比較令人滿意
func compressImageResource(data []byte) []byte { imgSrc, _, err := image.Decode(bytes.NewReader(data)) if err != nil { return data } newImg := image.NewRGBA(imgSrc.Bounds()) draw.Draw(newImg, newImg.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src) draw.Draw(newImg, newImg.Bounds(), imgSrc, imgSrc.Bounds().Min, draw.Over) buf := bytes.Buffer{} err = jpeg.Encode(&buf, newImg, &jpeg.Options{Quality: 40}) if err != nil { return data } if buf.Len() > len(data) { return data } return buf.Bytes() }
最后給大家分享一個超級好用PDF處理的golang 庫: https://github.com/unidoc/unipdf。一開始使用這個庫將生成后的PDF壓縮的,可以將一個200M的PDF(里面都是圖片)直接壓縮到7M左右??上У氖沁@個庫商用需要購買商業(yè)版權(quán),所以最后只能采取了導(dǎo)出前壓縮圖片的做法。
這個庫沒有授權(quán)的情況下會在處理后的PDF中加上水印,這個想去掉也簡單,fork下來改一下代碼就好了。雖然我這里因為是商業(yè)的場景不能這么用,但是我還是嘗試了下,倉庫在這:https://github.com/lianggx6/unipdf。然后再在go.mod文件中將依賴替換即可。大家如果有個人開發(fā)實踐需要的可以直接這樣拿來用,商用務(wù)必購買版權(quán)。
replace ( github.com/unidoc/unipdf/v3 => github.com/lianggx6/unipdf v0.0.0-20200409043947-1c871b2c4951 )
補充:golang中image/jpeg包和image/png包用法
jpeg包實現(xiàn)了jpeg圖片的編碼和解碼
func Decode(r io.Reader) (image.Image, error) //Decode讀取一個jpeg文件,并將他作為image.Image返回 func DecodeConfig(r io.Reader) (image.Config, error) //無需解碼整個圖像,DecodeConfig變能夠返回整個圖像的尺寸和顏色(Config具體定義查看gif包中的定義) func Encode(w io.Writer, m image.Image, o *Options) error //按照4:2:0的基準(zhǔn)格式將image寫入w中,如果options為空的話,則傳遞默認(rèn)參數(shù) type Options struct { Quality int }
Options是編碼參數(shù),它的取值范圍是1-100,值越高質(zhì)量越好
type FormatError //用來報告一個輸入不是有效的jpeg格式 type FormatError string func (e FormatError) Error() string type Reader //不推薦使用Reader type Reader interface { io.ByteReader io.Reader } type UnsupportedError func (e UnsupportedError) Error() string //報告輸入使用一個有效但是未實現(xiàn)的jpeg功能
利用程序畫一條直線,代碼如下:
package main import ( "fmt" "image" "image/color" "image/jpeg" "math" "os" ) const ( dx = 500 dy = 300 ) type Putpixel func(x, y int) func drawline(x0, y0, x1, y1 int, brush Putpixel) { dx := math.Abs(float64(x1 - x0)) dy := math.Abs(float64(y1 - y0)) sx, sy := 1, 1 if x0 >= x1 { sx = -1 } if y0 >= y1 { sy = -1 } err := dx - dy for { brush(x0, y0) if x0 == x1 && y0 == y1 { return } e2 := err * 2 if e2 > -dy { err -= dy x0 += sx } if e2 < dx { err += dx y0 += sy } } } func main() { file, err := os.Create("test.jpg") if err != nil { fmt.Println(err) } defer file.Close() nrgba := image.NewNRGBA(image.Rect(0, 0, dx, dy)) drawline(1, 1, dx-2, dy-2, func(x, y int) { nrgba.Set(x, y, color.RGBA{uint8(x), uint8(y), 0, 255}) }) for y := 0; y < dy; y++ { nrgba.Set(1, y, color.White) nrgba.Set(dx-1, y, color.White) } err = jpeg.Encode(file, nrgba, &jpeg.Options{100}) //圖像質(zhì)量值為100,是最好的圖像顯示 if err != nil { fmt.Println(err) } }
根據(jù)已經(jīng)得到的圖像test.jpg,我們創(chuàng)建一個新的圖像test1.jpg
package main import ( "fmt" "image/jpeg" "os" ) func main() { file, err := os.Open("test.jpg") if err != nil { fmt.Println(err) } defer file.Close() file1, err := os.Create("test1.jpg") if err != nil { fmt.Println(err) } defer file1.Close() img, err := jpeg.Decode(file) //解碼 if err != nil { fmt.Println(err) } jpeg.Encode(file1, img, &jpeg.Options{5}) //編碼,但是將圖像質(zhì)量從100改成5 }
對比圖像質(zhì)量為100和5的圖像:
image/png包用法:
image/png實現(xiàn)了png圖像的編碼和解碼
png和jpeg實現(xiàn)方法基本相同,都是對圖像進(jìn)行了編碼和解碼操作。
func Decode(r io.Reader) (image.Image, error) //Decode從r中讀取一個圖片,并返回一個image.image,返回image類型取決于png圖片的內(nèi)容 func DecodeConfig(r io.Reader) (image.Config, error) //無需解碼整個圖像變能夠獲取整個圖片的尺寸和顏色 func Encode(w io.Writer, m image.Image) error //Encode將圖片m以PNG的格式寫到w中。任何圖片都可以被編碼,但是哪些不是image.NRGBA的圖片編碼可能是有損的。 type FormatError func (e FormatError) Error() string //FormatError會提示一個輸入不是有效PNG的錯誤。 type UnsupportedError func (e UnsupportedError) Error() string //UnsupportedError會提示輸入使用一個合法的,但是未實現(xiàn)的PNG特性。
利用png包實現(xiàn)一個png的圖像,代碼如下:
package main import ( "fmt" "image" "image/color" "image/png" "os" ) const ( dx = 256 dy = 256 ) func Pic(dx, dy int) [][]uint8 { pic := make([][]uint8, dx) for i := range pic { pic[i] = make([]uint8, dy) for j := range pic[i] { pic[i][j] = uint8(i * j % 255) } } return pic } func main() { file, err := os.Create("test.png") if err != nil { fmt.Println(err) } defer file.Close() rgba := image.NewRGBA(image.Rect(0, 0, dx, dy)) for x := 0; x < dx; x++ { for y := 0; y < dy; y++ { rgba.Set(x, y, color.RGBA{uint8(x * y % 255), uint8(x * y % 255), 0, 255}) } } err = png.Encode(file, rgba) if err != nil { fmt.Println(err) } }
圖像如下:
由此可見,png和jpeg使用方法類似,只是兩種不同的編碼和解碼方式。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
golang 在windows中設(shè)置環(huán)境變量的操作
這篇文章主要介紹了golang 在windows中設(shè)置環(huán)境變量的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04go-zero熔斷機(jī)制組件Breaker接口定義使用解析
這篇文章主要為大家介紹了go-zero熔斷機(jī)制組件Breaker接口定義使用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05golang 項目打包部署環(huán)境變量設(shè)置方法
最近將 golang 項目打包部署在不同環(huán)境,下面分享一下我的心得體會,對golang 項目打包部署環(huán)境變量設(shè)置方法感興趣的朋友一起看看吧2024-07-07Windows+Linux系統(tǒng)下Go語言環(huán)境安裝配置過程
Go 語言被設(shè)計成一門應(yīng)用于搭載 Web 服務(wù)器,存儲集群或類似用途的巨型中央服務(wù)器的系統(tǒng)編程語言。這篇文章主要介紹了Windows+Linux系統(tǒng)下Go語言環(huán)境搭建配置過程,針對每種系統(tǒng)給大家講解的非常詳細(xì),需要的朋友可以參考下2021-06-06Go語言基礎(chǔ)函數(shù)包的使用學(xué)習(xí)
本文通過一個實現(xiàn)加減乘除運算的小程序來介紹go函數(shù)的使用,以及使用函數(shù)的注意事項,并引出了對包的了解和使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Golang創(chuàng)建第一個web項目(Gin+Gorm)
本文主要介紹了Golang創(chuàng)建第一個web項目(Gin+Gorm),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06