Golang压缩Jpeg图片和PNG图片的操作

时间:2022-06-01 19:08:58

博主一直在维护一个导出PDF的服务,但是这个服务导出的PDF文件是真的巨大,动辄就上百MB。这里面主要是图片占据了大多数体积,所以考虑在导出前压缩一下图片。

Jpeg的图片压缩是很好做的,因为jpeg这个协议本身就支持调整图片质量的。在golang中,我们只需要使用标准库的image/jpeg,将图片从二进制数据解码后,降低质量再编码为二进制数据即可实现压缩。而且质量和压缩比例相对而言还不错。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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图片,在网上找了很多相关的库,感觉都没什么即可以保持质量,又可以尽可能压缩的办法。

?
1
2
3
4
5
//下面这两个库都比较偏重于转换图片大小,在保持宽高不变的情况下,压缩比例很一般
https://github.com/discord/lilliput   //这个库是一家海外公司基于C语言的一个开源图片处理库,但是封装的很好,不需要安装额外依赖
https://github.com/disintegration/imaging
//下面这个库可以对PNG图片进行较大的压缩,可惜压缩比例过大时会严重失真
https://github.com/foobaz/lossypng/

后来,借鉴一篇博客的做法,还是先把PNG图片转换为Jpeg图片,然后再将jpeg图片的质量降低。相对上边这些库,压缩比例和质量都比较令人满意

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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左右。可惜的是这个库商用需要购买商业版权,所以最后只能采取了导出前压缩图片的做法。

这个库没有授权的情况下会在处理后的PDF中加上水印,这个想去掉也简单,fork下来改一下代码就好了。虽然我这里因为是商业的场景不能这么用,但是我还是尝试了下,仓库在这:https://github.com/lianggx6/unipdf。然后再在go.mod文件中将依赖替换即可。大家如果有个人开发实践需要的可以直接这样拿来用,商用务必购买版权。

?
1
2
3
replace (
 github.com/unidoc/unipdf/v3 => github.com/lianggx6/unipdf v0.0.0-20200409043947-1c871b2c4951
)

补充:golang中image/jpeg包和image/png包用法

jpeg包实现了jpeg图片的编码和解码

?
1
2
3
4
5
6
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的基准格式将image写入w中,如果options为空的话,则传递默认参数
type Options struct {
 Quality int
}

Options是编码参数,它的取值范围是1-100,值越高质量越好

?
1
2
3
4
5
6
7
8
9
10
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  //报告输入使用一个有效但是未实现的jpeg功能

利用程序画一条直线,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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})   //图像质量值为100,是最好的图像显示
 if err != nil {
 fmt.Println(err)
 }
}

根据已经得到的图像test.jpg,我们创建一个新的图像test1.jpg

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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}) //编码,但是将图像质量从100改成5
 
}

对比图像质量为100和5的图像:

Golang压缩Jpeg图片和PNG图片的操作

image/png包用法:

image/png实现了png图像的编码和解码

png和jpeg实现方法基本相同,都是对图像进行了编码和解码操作。

?
1
2
3
4
5
6
7
func Decode(r io.Reader) (image.Image, error)   //Decode从r中读取一个图片,并返回一个image.image,返回image类型取决于png图片的内容
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会提示输入使用一个合法的,但是未实现的PNG特性。

利用png包实现一个png的图像,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)
 }
}

图像如下:

Golang压缩Jpeg图片和PNG图片的操作

由此可见,png和jpeg使用方法类似,只是两种不同的编码和解码方式。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://www.cnblogs.com/lianggx6/p/12684885.html