- 缩放图像包
"github.com/disintegration/imaging"
src, err := imaging.Open("1.jpg", imaging.AutoOrientation(true))//自动更改方向EXIT信息
src = imaging.Resize(src, 0, 400, imaging.Lanczos)//缩放图像,宽高只能有一个0表示自动比例
src=imaging.Thumbnail(src,200,200,imaging.Lanczos)//缩略图生成
src = imaging.Blur(src,3) //模糊处理3以上更模糊
src = imaging.Sharpen(src,3)//锐化3以上
src = imaging.AdjustGamma(src,2)//添加亮度,数值越大越亮,1以上更亮
src = imaging.Grayscale(src)//灰阶
src = imaging.Invert(src)//反色
src=imaging.FlipH(src) //垂直翻转
src=imaging.FlipV(src)//水平翻转
src=imaging.Rotate(src,45,color.NRGBA{0, 0, 0, 0}) //旋转图片支持任意角度0-360
err = imaging.Save(src, "x.jpg")//保存图像并命名
dst := imaging.New(400, 400, color.NRGBA{0, 0, 0, 0})//新建图像
dst = imaging.Paste(dst, src, image.Pt(0, 0))//拷贝图像
"github.com/o1egl/govatar" //生成像素头像
img, _ := govatar.Generate(govatar.MALE)
r.Header.Set("Content-Type","image/jpeg")
jpeg.Encode(r.Response.Writer,img,nil)
img, _ := govatar.GenerateForUsername(govatar.FEMALE,"白") 唯一头像
生成文件
err := govatar.GenerateFile(govatar.MALE, "/path/to/avatar.jpg")
err := govatar.GenerateFileFromUsername(govatar.MALE, "username", "/path/to/avatar.jpg")
- 生成图片
"github.com/golang/freetype"
w:=r.GetInt("w",200)
h:=r.GetInt("h",200)
rectImage := image.NewRGBA(image.Rect(0, 0, w, h))
green := color.RGBA{4, 190, 2, 70} //背景颜色
draw.Draw(rectImage, rectImage.Bounds(), &image.Uniform{green}, image.Point{}, draw.Src)
c := freetype.NewContext()
c.SetDst(rectImage)
c.SetClip(rectImage.Bounds())
c.SetSrc(image.Black)
c.SetFontSize(25)//字体大小
fontBytes, _ := ioutil.ReadFile("f.ttf")//载入字体
font, _ := freetype.ParseFont(fontBytes)
c.SetFont(font)
c.DrawString(strconv.Itoa(w)+" x "+strconv.Itoa(h), freetype.Pt(20,h/2))
//file, _ := os.Create(strconv.Itoa(w)+strconv.Itoa(h)+".png")
//png.Encode(file, rectImage) //生成图片
r.Header.Set("Content-Type","image/png")
png.Encode(r.Response.Writer,rectImage)//输出到浏览器
- 图片处理
"github.com/fogleman/gg"
dc := gg.NewContext(500,500)
dc.DrawCircle(250, 250, 200)//画圆
dc.SetRGB(1, 0, 0)//设置颜色
dc.Fill()
dc.SavePNG("out.png")//输出图片
dc.EncodePNG(r.Response.Writer)//输出到浏览器
SaveJPG("out.jpg",dc.Image())//jpeg
im, _ := gg.LoadImage("qr.png") //加载图像
w := im.Bounds().Size().X
h := im.Bounds().Size().Y
dc := gg.NewContext(w,h)
dc.DrawImage(im,0,0)//复制图像
dc.Clip()//剪切
dc.SetRGB(0, 0, 0)//字体颜色
dc.LoadFontFace("zk.ttf",25)//字体和大小
dc.DrawString("测试", 0, 100)//内容和位置
s := "测试"
sWidth, sHeight := dc.MeasureString(s)
dc.DrawString(s, (float64(w)-sWidth)/2, (float64(h)+sHeight)/2)//居中
dc.DrawStringWrapped("我是你的朋友 我喜欢你是我朋友",120,120,0.5,0.5,255,1.5,gg.AlignCenter)//写入中文换行
//x,y,0.5,0.5,宽度,1.5是行高,空格是换行
dc.RotateAbout(gg.Radians(45), float64(w/2), float64(h/2))//旋转45°
func SaveJPG(path string, im image.Image) error {//输出到jpeg
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return jpeg.Encode(file, im,&jpeg.Options{99})
}
- 二维码生成 qrcode “github.com/skip2/go-qrcode”
png,_:= qrcode.Encode("https://example.org", qrcode.Medium, 256) fmt.Println(png) qrcode.WriteFile("https://example.org", qrcode.Medium, 256, "qr.png") qrcode.WriteColorFile("彩色二维码", qrcode.Medium, 256, color.RGBA{11,22,44,90}, color.White, "qr1.png")
文档更新时间: 2024-10-22 03:42 作者:Yoby