This is a creation in Article, where the information may have evolved or changed.
JPEG package enables encoding and decoding of JPEG images
Func Decode (R io. Reader) (image. Image, error)//decode reads a JPEG file and takes him as an image. Image returns
Func decodeconfig (R io. Reader) (image. Config, error)//without decoding the entire image, Decodeconfig can return the size and color of the entire image (config-specific definition to see the definition in the GIF package)
Func Encode (w io. Writer, M image. Image, O *options) error//write image to W according to 4:2:0 's base format, pass default parameter if Options are empty
Type Options struct {quality int}
Options is the encoding parameter, its value range is 1-100, the higher the value, the better the quality
Type formaterror//used to report that an input is not a valid JPEG format
Type Formaterror string
Func (e formaterror) Error () string
Type reader//not recommended to use reader
Type Reader interface {IO. Bytereaderio.reader}
Type Unsupportederror
Func (e unsupportederror) Error () string//Report input using a valid but not implemented JPEG function
Use the program to draw a line, the code is as follows:
Package Mainimport ("FMT" "Image" "Image/color" "Image/jpeg" "Math" "OS") const (dx = 500dy = +) Type Putpixel func (x, y int) f UNC DrawLine (x0, y0, x1, y1 int, brush putpixel) {dx: = Math. Abs (Float64 (x1-x0)) dy: = Math. Abs (Float64 (y1-y0)) SX, SY: = 1, 1if x0 >= x1 {sx = -1}if y0 >= y1 {sy = -1}err: = dx-dyfor {Brush (x0, y0) if x0 = = x1 && y0 = y1 {return}e2: = Err * 2if e2 >-dy {err-= dyx0 + sx}if E2 < DX {err + dxy0 + = Sy}}}func ma In () {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}) //image quality value of 100, is the best image to display if err! = Nil {fmt. PRINTLN (ERR)}}
Based on the obtained image test.jpg, we create a new image test1.jpg
Package Mainimport ("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)//decode if err! = Nil {fmt. PRINTLN (Err)}jpeg. Encode (File1, IMG, &jpeg. OPTIONS{5})//encode, but change the image quality from 100 to 5}
Compare images with image quality of 100 and 5:
Image with a mass of 100
Image with a mass of 5
Image/png Package Usage:
Image/png enables the encoding and decoding of PNG images
PNG and JPEG implementations are basically the same, both encode and decode the image.
Func Decode (R io. Reader) (image. Image, error)//decode reads a picture from R and returns a image.image that returns the image type depending on the contents of the PNG picture
Func decodeconfig (R io. Reader) (image. Config, error)//without decoding the entire image to get the size and color of the entire picture
Func Encode (w io. Writer, M image. Image) Error//encode writes the picture m in PNG format to W. Any image can be encoded, but which is not an image. The image encoding of the Nrgba may be lossy.
Type Formaterror
The Func (e formaterror) error () string//formaterror prompts an error that the input is not a valid PNG.
Type Unsupportederror
The Func (e unsupportederror) Error () string//unsupportederror prompts for the use of a valid, but not implemented, PNG attribute.
Use the PNG package to implement a PNG image with the following code:
Package Mainimport ("FMT" "Image" "Image/color" "Image/png" "OS") const (DX = 256dy = $) 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)}}
The image is as follows:
This shows that PNG and JPEG use similar methods, just two different encoding and decoding methods.
Reference:
Grasshopper-June's Blog: http://www.cnblogs.com/ghj1976/p/3441536.html
Golang Technology official Website: http://docscn.studygolang.com/pkg/image/jpeg/