golang中image/jpeg包和image/png包用法

jpeg包實現了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的基準格式將image寫入w中,如果options爲空的話,則傳遞默認參數

type Options struct {
	Quality int
}
Options是編碼參數,它的取值範圍是1-100,值越高質量越好

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功能

利用程序畫一條直線,代碼如下:

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

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的圖像:

                                           質量爲100的圖像

                                                      質量爲5的圖像


image/png包用法:

image/png實現了png圖像的編碼和解碼

png和jpeg實現方法基本相同,都是對圖像進行了編碼和解碼操作。

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的圖像,代碼如下:

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使用方法類似,只是兩種不同的編碼和解碼方式。


參考:

蟈蟈俊的博客:http://www.cnblogs.com/ghj1976/p/3441536.html

golang技術官網:http://docscn.studygolang.com/pkg/image/jpeg/


發佈了41 篇原創文章 · 獲贊 24 · 訪問量 61萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章