go實(shí)現(xiàn)圖片拼接與文字書(shū)寫(xiě)的方法實(shí)例
零:背景
這是我工作中實(shí)際碰到的后端生成圖片拼接和文字貼圖需求。特此總結(jié)下來(lái),方便后人。文中代碼都是我們生產(chǎn)環(huán)境使用的。
一:圖片拼接
go標(biāo)準(zhǔn)庫(kù)的image包本身就能實(shí)現(xiàn)拼接,因此還是比較簡(jiǎn)單的
直接上代碼
1.1 圖片拼接代碼
//圖片拼接 func MergeImageNew(base image.Image, mask image.Image, paddingX int, paddingY int) (*image.RGBA, error) { baseSrcBounds := base.Bounds().Max maskSrcBounds := mask.Bounds().Max newWidth := baseSrcBounds.X newHeight := baseSrcBounds.Y maskWidth := maskSrcBounds.X maskHeight := maskSrcBounds.Y des := image.NewRGBA(image.Rect(0, 0, newWidth, newHeight)) // 底板 //首先將一個(gè)圖片信息存入jpg draw.Draw(des, des.Bounds(), base, base.Bounds().Min, draw.Over) //將另外一張圖片信息存入jpg draw.Draw(des, image.Rect(paddingX, newHeight-paddingY-maskHeight, (paddingX+maskWidth), (newHeight-paddingY)), mask, image.ZP, draw.Over) return des, nil }
核心就是使用image>newRGBA新建一個(gè)空白底圖,讓后將背景圖,拼接圖使用draw.Draw畫(huà)上去就好了。
1.2 從本地、網(wǎng)絡(luò)讀取圖片
從本地讀取
func GetImageFromFile(filePath string) (img image.Image, err error) { f1Src, err := os.Open(filePath) if err != nil { return nil, err } defer f1Src.Close() buff := make([]byte, 512) // why 512 bytes ? see http://golang.org/pkg/net/http/#DetectContentType _, err = f1Src.Read(buff) if err != nil { return nil, err } filetype := http.DetectContentType(buff) fmt.Println(filetype) fSrc, err := os.Open(filePath) defer fSrc.Close() switch filetype { case "image/jpeg", "image/jpg": img, err = jpeg.Decode(fSrc) if err != nil { fmt.Println("jpeg error") return nil, err } case "image/gif": img, err = gif.Decode(fSrc) if err != nil { return nil, err } case "image/png": img, err = png.Decode(fSrc) if err != nil { return nil, err } default: return nil, err } return img, nil }
從網(wǎng)絡(luò)中讀取
func GetImageFromNet(url string) (image.Image, error) { res, err := http.Get(url) if err != nil || res.StatusCode != 200 { return nil, err } defer res.Body.Close() m, _, err := image.Decode(res.Body) return m, err }
保存圖片
func SaveImage(targetPath string, m image.Image) error { fSave, err := os.Create(targetPath) if err != nil { return err } defer fSave.Close() err = jpeg.Encode(fSave, m, nil) if err != nil { return err } return nil }
二:文字書(shū)寫(xiě)
圖片書(shū)寫(xiě)文字是基于 github.com/golang/freetype 這個(gè)庫(kù)實(shí)現(xiàn)的
import ( "github.com/golang/freetype" "github.com/golang/freetype/truetype" "golang.org/x/image/font" "image" "io/ioutil" ) //字體相關(guān) type TextBrush struct { FontType *truetype.Font FontSize float64 FontColor *image.Uniform TextWidth int } func NewTextBrush(FontFilePath string, FontSize float64, FontColor *image.Uniform, textWidth int) (*TextBrush, error) { fontFile, err := ioutil.ReadFile(FontFilePath) if err != nil { return nil, err } fontType, err := truetype.Parse(fontFile) if err != nil { return nil, err } if textWidth <= 0 { textWidth = 20 } return &TextBrush{FontType: fontType, FontSize: FontSize, FontColor: FontColor, TextWidth: textWidth}, nil } // 圖片插入文字 func (fb *TextBrush) DrawFontOnRGBA(rgba *image.RGBA, pt image.Point, content string) { c := freetype.NewContext() c.SetDPI(72) c.SetFont(fb.FontType) c.SetHinting(font.HintingFull) c.SetFontSize(fb.FontSize) c.SetClip(rgba.Bounds()) c.SetDst(rgba) c.SetSrc(fb.FontColor) c.DrawString(content, freetype.Pt(pt.X, pt.Y)) } func Image2RGBA(img image.Image) *image.RGBA { baseSrcBounds := img.Bounds().Max newWidth := baseSrcBounds.X newHeight := baseSrcBounds.Y des := image.NewRGBA(image.Rect(0, 0, newWidth, newHeight)) // 底板 //首先將一個(gè)圖片信息存入jpg draw.Draw(des, des.Bounds(), img, img.Bounds().Min, draw.Over) return des }
使用example
func TestTextBrush_DrawFontOnRGBA(t *testing.T) { textBrush, err := NewTextBrush("字體庫(kù)ttf位置", 20, image.Black, 20) if err != nil { t.Log(err) } backgroud, err := GetImageFromFile("./resource/backgroud.jpg") if err != nil { t.Log(err) } des := Image2RGBA(backgroud) textBrush.DrawFontOnRGBA(des, image.Pt(10, 50), "世界你好") //調(diào)整顏色 textBrush.FontColor = image.NewUniform(color.RGBA{ R: 0x8E, G: 0xE5, B: 0xEE, A: 255, }) textBrush.DrawFontOnRGBA(des, image.Pt(10, 80), "我是用Go拼上的文字") if err := SaveImage("./resource/text.png", des); err != nil { t.Log(err) } }
先使用NewTextBrush第一個(gè)參數(shù)是字體庫(kù)文件位置。這里使用的ttf格式的字體庫(kù),網(wǎng)上應(yīng)該有免費(fèi)的字體庫(kù)。
參考我的example中的代碼就可以直接使用。
總結(jié)
到此這篇關(guān)于go實(shí)現(xiàn)圖片拼接與文字書(shū)寫(xiě)的文章就介紹到這了,更多相關(guān)go實(shí)現(xiàn)圖片拼接文字書(shū)寫(xiě)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語(yǔ)言循環(huán)遍歷含有中文的字符串的方法小結(jié)
這篇文章主要介紹了Go語(yǔ)言循環(huán)遍歷含有中文的字符串的幾種方法,文章通過(guò)代碼示例講解的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴跟著小編一起來(lái)看看吧2023-07-07GO的基礎(chǔ)知識(shí)掃盲注意事項(xiàng)
這篇文章主要介紹了GO的基礎(chǔ)知識(shí)注意事項(xiàng),本文是GO語(yǔ)言小白的掃盲文,主要講解了go語(yǔ)言的基本知識(shí),GO程序目錄結(jié)構(gòu),GO程序包的導(dǎo)入與別名運(yùn)用,GO內(nèi)置關(guān)鍵字,GO注釋方法需要的朋友可以參考下2022-12-12一文詳解golang延時(shí)任務(wù)的實(shí)現(xiàn)
這篇文章主要為大家介紹了golang延時(shí)任務(wù)的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03vscode搭建go開(kāi)發(fā)環(huán)境案例詳解
對(duì)于Visual Studio Code開(kāi)發(fā)工具,有一款優(yōu)秀的GoLang插件,今天通過(guò)本文給大家介紹下vscode搭建go開(kāi)發(fā)環(huán)境的詳細(xì)教程,感興趣的朋友跟隨小編一起看看吧2021-12-12