Image/gif Package usage in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

GIF package enables decoding and encoding of GIF images

Func Decode (R io. Reader) (image. Image, error)//decode reads a GIF image from R, and then returns the image. The image is the first embedded diagram.


Func decodeconfig (R io. Reader) (image. Config, error)//decodeconfig does not need to decode the entire image to return the global color model and the size of the GIF image.

Type Config struct {    ColorModel    color. Model    Width, Height int}
Config returns the image's color model and size

Func Encode (w io. Writer, M image. Image, O *options) error//To write picture m in GIF mode W

The type Options struct {//numcolors is the maximum color used in a picture, and its range is 1-256numcolors int//Quantizer is often used to generate palettes through numcolors, palette. Plan9 is used to replace nil Quantizerquantizer draw. quantizer//Drawer i is used to convert the source picture into the desired palette, draw. Floydsteinberg is used instead of an empty drawer.drawer draw. Drawer}


Func encodeall (w io. Writer, g *gif) error//The picture is written in W according to the number of cycles and delays specified between frames and frames

Type GIF struct {    Image     []*image. paletted//Continuous picture    delay     []int             //continuous delay time, each frame unit is 1% seconds, the value in Delay indicates the time interval of its two images dynamic display    Loopcount int               / /cycle times, if 0 is always cycled. }

Func decodeall (R io. Reader) (*gif, error)//decodeall reads a GIF picture from R and returns the frame and time information for the order

A simple example of how to make a GIF image with a GIF package, draw two vertical intersecting dynamic plots (if you want to get a complex GIF image, you can set more complex drawing lines and color patterns to get complex graphics):

Package Mainimport ("FMT" "Image" "Image/color" "Image/color/palette" "Image/gif" "Net/http" "OS") Func Main () {http. Handlefunc ("/display", display) Err: = http. Listenandserve (": 9100", nil) if err! = Nil {fmt. PRINTLN (ERR)}}func display (W http. Responsewriter, Q *http. Request) {f1, err: = OS. Create ("Test.gif") if err! = Nil {fmt. PRINTLN (Err)}defer F1. Close () P1: = image. newpaletted (image. Rect (0, 0, palette),. PLAN9) for x: = 0; x < 100; X + + {for y: = 0; y < y++ {P1. Set (x, y, color. Rgba{uint8 (x), Uint8 (y), 255, 255})}}p2: = image. newpaletted (image. Rect (0, 0, Palette, a),. PLAN9) for x: = 0; x < 100; X + + {for y: = 0; y < y++ {P2. Set (x, a, color. Rgba{uint8 (x * x% 255), uint8 (y * y% 255), 0, 255})}}g1: = &gif. Gif{image:     []*image. PALETTED{P1, P2},delay:     []int{30, 30},loopcount:0,}gif. Encodeall (W, G1)  //browser displays GIF. Encodeall (F1, G1)//save to file}

Animated GIF images are as follows:


Of course, you can also use the existing image to generate GIF, the code is as follows:

Package Mainimport ("FMT" "Image" "Image/color/palette" "Image/draw" "Image/gif" "Image/jpeg" "Image/png" "Net/http" "Os ") func Main () {http. Handlefunc ("/display", display) Err: = http. Listenandserve (": 9100", nil) if err! = Nil {fmt. PRINTLN (ERR)}}func display (W http. Responsewriter, Q *http. Request) {f, err: = OS. Open ("Test.jpeg") if err! = Nil {fmt. PRINTLN (Err)}defer f.close () g, err: = JPEG. Decode (f) if err! = Nil {fmt. PRINTLN (Err)}f2, err: = OS. Open ("123.png") if err! = Nil {fmt. PRINTLN (Err)}defer f.close () g2, err: = png. Decode (F2) if err! = Nil {fmt. PRINTLN (Err)}f1, err: = OS. Create ("Test.gif") if err! = Nil {fmt. PRINTLN (Err)}defer F1. Close () P1: = image. newpaletted (image. Rect (0, 0, $, palette). PLAN9) draw. Draw (P1, p1. Bounds (), G, image. ZP, Draw. SRC)//Add picture P2: = image. newpaletted (image. Rect (0, 0, $, palette). PLAN9) draw. Draw (P2, p2. Bounds (), G2, image. ZP, Draw. SRC)//Add picture g1: = &gif. Gif{image: []*image. PALETTED{P1, P2},delay: []int{30, 30},loopcount:0,}gif. Encodeall (W, G1) GIF.encodeall (F1, G1)} 
The resulting GIF picture is as follows:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.