golang图片处理库image基本操作

Lida ·
更新时间:2024-09-20
· 450 次阅读

目录

基本操作

读取

新建

保存

图片修改

转换

裁剪

缩放

基本操作

图片的基本读取与保存。

读取

图片读取和文件读取类似,需要先获取流:

注册图片的解码器(如:jpg则import _ "image/jpeg", png则import _ "image/png"

通过os.open打开文件获取流;

通过image.Decode解码流,获取图片;

import _ "image/jpeg" func readPic() image.Image { f, err := os.Open("C:\\hatAndSunglass.jpg") if err != nil { panic(err) } defer f.Close() img, fmtName, err := image.Decode(f) if err != nil { panic(err) } fmt.Printf("Name: %v, Bounds: %+v, Color: %+v", fmtName, img.Bounds(), img.ColorModel()) return img }

解码后返回的第一个参数为Image接口:

type Image interface { ColorModel() color.Model // 返回图片的颜色模型 Bounds() Rectangle // 返回图片外框 At(x, y int) color.Color // 返回(x,y)像素点的颜色 } 新建

新建一个图片非常简单,只需image.NewRGBA即可创建一个透明背景的图片了

img := image.NewRGBA(image.Rect(0, 0, 300, 300)) 保存

保存图片也很简单,需要编码后,写入文件流即可:

注册图片的解码器

通过os.create创建文件;通

png.Encode编码图片并写入文件;

func savePic(img *image.RGBA) { f, err := os.Create("C:\\tmp.jpg") if err != nil { panic(err) } defer f.Close() b := bufio.NewWriter(f) err = jpeg.Encode(b, img, nil) if err != nil { panic(err) } b.Flush() } 图片修改

很多操作都需要用到绘制图片:

func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op) func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op)​

主要参数说明:

dst:绘图的背景图

r:背景图的绘图区域

src:要绘制的图

sp:要绘制图src的开始点

op:组合方式

DrawMask多了一个遮罩蒙层参数,Draw为其一种特殊形式(遮罩相关参数为nil)。

转换

读取的jpg图像不是RGBA格式的(为YCbCr格式);在操作前需要先转换格式:

创建一个大小相同的RGBA图像;

把jpg画到新建的图像上去;

func jpg2RGBA(img image.Image) *image.RGBA { tmp := image.NewRGBA(img.Bounds()) draw.Draw(tmp, img.Bounds(), img, img.Bounds().Min, draw.Src) return tmp } 裁剪

通过subImage方法可方便地裁剪图片(需要为RGBA格式的)

func subImg() { pic := readPic() fmt.Printf("Type: %T\n", pic) img := jpg2RCBA(pic) sub := img.SubImage(image.Rect(0, 0, pic.Bounds().Dx(), pic.Bounds().Dy()/2)) savePic(sub.(*image.RGBA)) } 缩放

图片缩放分为保持比例与不保持比例的缩放;保持比例时,要确定新图片的位置(是否居中),以及如何填充空白处。
为了缩放,需要引入新的库golang.org/x/image/draw

在保持比例缩放时,需要先计算缩放后的图片大小:

分别计算宽、高的缩放比例,以小者为准;

若是居中(否则靠左上)需要计算填充大小,然后据此计算位置;

func calcResizedRect(width int, src image.Rectangle, height int, centerAlign bool) image.Rectangle { var dst image.Rectangle if width*src.Dy() < height*src.Dx() { // width/src.width < height/src.height ratio := float64(width) / float64(src.Dx()) tH := int(float64(src.Dy()) * ratio) pad := 0 if centerAlign { pad = (height - tH) / 2 } dst = image.Rect(0, pad, width, pad+tH) } else { ratio := float64(height) / float64(src.Dy()) tW := int(float64(src.Dx()) * ratio) pad := 0 if centerAlign { pad = (width - tW) / 2 } dst = image.Rect(pad, 0, pad+tW, height) } return dst }

有了缩放后的大小后,即可通过双线性插值bilinear的方式进行图片的缩放

img为要缩放的图片width、height为缩放后的大小

keepRatio为是否保持比例缩放

fill为填充的颜色(R、G、B都为fill)

centerAlign:保持比例缩放时,图片是否居中存放

import ( "image" "image/color" "golang.org/x/image/draw" ) func resizePic(img image.Image, width int, height int, keepRatio bool, fill int, centerAlign bool) image.Image { outImg := image.NewRGBA(image.Rect(0, 0, width, height)) if !keepRatio { draw.BiLinear.Scale(outImg, outImg.Bounds(), img, img.Bounds(), draw.Over, nil) return outImg } if fill != 0 { fillColor := color.RGBA{R: uint8(fill), G: uint8(fill), B: uint8(fill), A: 255} draw.Draw(outImg, outImg.Bounds(), &image.Uniform{C: fillColor}, image.Point{}, draw.Src) } dst := calcResizedRect(width, img.Bounds(), height, centerAlign) draw.ApproxBiLinear.Scale(outImg, dst.Bounds(), img, img.Bounds(), draw.Over, nil) return outImg }

到此这篇关于golang图片处理库image简介的文章就介绍到这了,更多相关golang图片处理库image内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



image golang

需要 登录 后方可回复, 如果你还没有账号请 注册新账号