[TOC] > [github](https://github.com/disintegration/imaging) ## 安装 ``` go get -u github.com/disintegration/imaging ``` ## 打开图片/ 输出图片 ``` //打开 src, err := imaging.Open("test/branches.png") //保存 err = imaging.Save(dst, "test/out_example.jpg") ``` ## 缩放/剪切图片的几种方式 ``` img1 := imaging.Resize(src, 200, 200, imaging.Lanczos) //重置大小 img2 := imaging.Fit(src, 600, 200, imaging.Box) //适应指定的/最大宽度和高度 img3 := imaging.Fill(src, 200, 200, imaging.Center, imaging.Lanczos) //通过中心截取 img3 := imaging.CropAnchor(src, 200, 200, imaging.Center) //剪切图片,从中心截取200 大小的图 ``` ## 缩略图 ``` imaging.Thumbnail(img1, 100, 100, imaging.Lanczos) ``` ## 调整模糊度 ``` dstImage := imaging.Blur(img1, 0.5) ``` ## 调整对比度 ``` imaging.AdjustContrast(img1, 20) //范围 -100 ~ 100 ``` ## 明暗度 ``` imaging.AdjustBrightness(img1, 20) ``` ## 反向像素产生一个图片 ``` imaging.Invert(img1) ``` ## 旋转 ``` img1 := imaging.FlipH(src) //左右颠倒 img1 := imaging.FlipV(src) //上下颠倒 img1 := imaging.Transpose(src) //逆时针旋转 90度 //同 imaging.Rotate90(src) img1 := imaging.Transverse(src) //顺时针旋转9度 imaging.Rotate180 //逆时针180度 imaging.Rotate270 //逆时针270度 ``` ## 并接多张图片为一张 ``` //打开图片 src, err := imaging.Open("test/branches.png") if err != nil { log.Fatalf("failed to open image: %v", err) } img1 := imaging.Resize(src, 200, 200, imaging.Lanczos) img2 := imaging.Fit(src, 600, 200, imaging.Box) img3 := imaging.Fill(src, 200, 200, imaging.Center, imaging.Lanczos) img4 := imaging.Resize(src, 200, 200, imaging.Box) dst := imaging.New(400, 400, color.NRGBA{0, 0, 0, 0}) //填充颜色为黑色 dst = imaging.Paste(dst, img4, image.Pt(0, 0)) dst = imaging.Paste(dst, img1, image.Pt(0, 200)) dst = imaging.Paste(dst, img2, image.Pt(200, 0)) dst = imaging.Paste(dst, img3, image.Pt(200, 200)) // 输出图片 err = imaging.Save(dst, "test/out_example.jpg") if err != nil { log.Fatalf("failed to save image: %v", err) } ```