Image/draw Package usage in Golang

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

The draw package provides a way to create a picture, or a drawing method. How to use it simply import the response package to "import Image/draw"


Func Draw (DST image, R image. Rectangle, src image. Image, SP image. Point, Op op)

Draw is a special form of drawmask, which is the draw function when the mask is nil in the Drawmask method
Func Drawmask (DST image, R image. Rectangle, src image. Image, SP image. Point, mask image. Image, MP image. Point, Op op)

The meanings of each of these parameters are as follows:

  the background map for the DST drawing.    R is the drawing area of the background map    src is the diagram to be drawn the    SP is src corresponding to the drawing start point (the drawn size R variable defines)    mask is the mask used when drawing, controlling how the picture is replaced.  The    MP is the drawing-time mask at the start point (the size of the R variable is drawn defined) Op op is a porter-duff compositing operator. Reference article: "http://blog.csdn.net/ison81/article/details/5468763"  http://blog.csdn.net/ison81/article/details/ 5468763 Porter-duff Equation 12 rules can read this blog: "http://www.blogjava.net/onedaylover/archive/2008/01/16/175675.html" http// Www.blogjava.net/onedaylover/archive/2008/01/16/175675.html

Drawmask aligns the MP on the Sp,mask on the r.min,src on DST and then performs a porter-duff merge operation on the R-type matrix region on DST. Mask set to nil represents completely opaque.


Type Drawer//drawer contains only one draw function

Type Drawer Interface {//Draw aligns the r.min in DST according to the SP in SRC, and then replaces the rectangle Rdraw (DST Image, R image) with the result of drawing src//on DST. Rectangle, src image. Image, SP image. Point)}


Type Image

Type Image interface {image. ImageSet (x, y int, C color.) Color)}
Draw. Image is really image.image based on a set method to change a single pixel

Type Op

Type Op int, which is enabled in the draw and Drawmask methods in the const (//Over description "(SRC on mask) overlay on DST".    OVER Op = iota//src description ' ' src Action on mask '. SRC)
Func (Op op) Draw (DST image, R image. Rectangle, src image. Image, SP image. Point)//draw implements the Drawer interface by invoking the Draw function with this Op.


Type Quantizer

Type Quantizer Interface {//quantize appends up to caps (p)-Len (p) colors to p and returns the//updated palette suitable For converting m to a paletted image. Quantize (P color. Palette, M image. Image) color. Palette}

Illustrate the use of the draw package:

Original Picture:


To capture part of a picture:

Package Mainimport ("FMT" "Image" "Image/color" "Image/draw" "Image/jpeg" "OS") Func main () {file, err: = OS. Create ("dst.jpg") if err! = Nil {fmt. PRINTLN (Err)}defer file. Close () file1, err: = OS. Open ("20.jpg") if err! = Nil {fmt. PRINTLN (Err)}defer file1. Close () img, _: = JPEG. Decode (file1) jpg: = image. Newrgba (image. Rect (0, 0, ()) draw. Draw (jpg, img. Bounds (). ADD (image. Pt (Ten)), IMG, IMG. Bounds (). Min, Draw. SRC)//Capture part of the image as JPEG. Encode (file, jpg, nil)}
Image after capture:


Convert the original picture to a gray picture:

Package Mainimport ("FMT" "Image" "Image/color" "Image/draw" "Image/jpeg" "OS") Func main () {file, err: = OS. Create ("dst.jpg") if err! = Nil {fmt. PRINTLN (Err)}defer file. Close () file1, err: = OS. Open ("20.jpg") if err! = Nil {fmt. PRINTLN (Err)}defer file1. Close () img, _: = JPEG. Decode (file1) jpg: = image. Newgray (IMG. Bounds ())                                 //newgraydraw.draw (jpg, jpg. Bounds (), IMG, IMG. Bounds (). Min, Draw. SRC)//original picture converted to gray image jpeg. Encode (file, jpg, nil)}
After conversion:


Use draw to achieve two image compositing:

Func Main () {file, err: = OS. Create ("dst.jpg") if err! = Nil {fmt. PRINTLN (Err)}defer file. Close () file1, err: = OS. Open ("20.jpg") if err! = Nil {fmt. PRINTLN (Err)}defer file1. Close () img, _: = JPEG. Decode (file1) file2, err: = OS. Open ("10.jpg") if err! = Nil {fmt. PRINTLN (Err)}defer file2. Close () Img2, _: = JPEG. Decode (file2) jpg: = image. Newrgba (image. Rect (0, 0, +)) draw. Draw (jpg, jpg. Bounds (), Img2, Img2. Bounds (). Min, Draw. OVER)                   //First put a picture information into Jpgdraw. Draw (jpg, jpg. Bounds (), IMG, IMG. Bounds (). Min.sub (image. Pt (0, 0)), draw. OVER)   //Add another picture information to jpg//draw. Drawmask (jpg, jpg. Bounds (), IMG, IMG. Bounds (). Min, Img2, Img2. Bounds (). Min, Draw. SRC)//Use this method to not directly synthesize two images? The reason is not yet known. Jpeg. Encode (file, jpg, nil)}

Post-synthesis pictures are as follows:



Reference:

Grasshopper-June Blog: http://www.cnblogs.com/ghj1976/p/3443638.html

Golang Official website: http://docscn.studygolang.com/pkg/image/draw/




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.