SoFunction
Updated on 2025-03-05

Examples of how to implement image splicing and text writing

Zero: Background

This is the backend generated image stitching and text map requirements I actually encountered at work. Let me summarize it here to facilitate future generations. The code in the article is used in our production environment.

1: Picture splicing

The image package of the go standard library can be spliced ​​by itself, so it is relatively simple

Directly upload the code

1.1 Picture splicing code

//Picture splicingfunc MergeImageNew(base , mask , paddingX int, paddingY int) (*, error) {
	baseSrcBounds := ().Max

	maskSrcBounds := ().Max

	newWidth := 
	newHeight := 

	maskWidth := 
	maskHeight := 

	des := ((0, 0, newWidth, newHeight)) // Bottom plate	//First save an image information into jpg	(des, (), base, ().Min, )
	//Save another image information into jpg	(des, (paddingX, newHeight-paddingY-maskHeight, (paddingX+maskWidth), (newHeight-paddingY)), mask, , )

	return des, nil
}

The core is to use image>newRGBA to create a new blank background image, and then use the background image and the stitching image to draw it.

1.2 Read pictures from local and network

Read from local

func GetImageFromFile(filePath string) (img , err error) {
	f1Src, err := (filePath)

	if err != nil {
		return nil, err
	}
	defer ()

	buff := make([]byte, 512) // why 512 bytes ? see /pkg/net/http/#DetectContentType
	_, err = (buff)

	if err != nil {
		return nil, err
	}

	filetype := (buff)

	(filetype)

	fSrc, err := (filePath)
	defer ()

	switch filetype {
	case "image/jpeg", "image/jpg":
		img, err = (fSrc)
		if err != nil {
			("jpeg error")
			return nil, err
		}

	case "image/gif":
		img, err = (fSrc)
		if err != nil {
			return nil, err
		}

	case "image/png":
		img, err = (fSrc)
		if err != nil {
			return nil, err
		}
	default:
		return nil, err
	}
	return img, nil
}

Read from the network

func GetImageFromNet(url string) (, error) {
	res, err := (url)
	if err != nil ||  != 200 {
		return nil, err
	}
	defer ()
	m, _, err := ()
	return m, err
}

Save the picture

func SaveImage(targetPath string, m ) error {
	fSave, err := (targetPath)
	if err != nil {
		return err
	}
	defer ()

	err = (fSave, m, nil)

	if err != nil {
		return err
	}

	return nil
}

Two: Writing

The text of the picture is implemented based on the /golang/freetype library

import (
	"/golang/freetype"
	"/golang/freetype/truetype"
	"/x/image/font"
	"image"
	"io/ioutil"
)

//Font relatedtype TextBrush struct {
	FontType  *
	FontSize  float64
	FontColor *
	TextWidth int
}

func NewTextBrush(FontFilePath string, FontSize float64, FontColor *, textWidth int) (*TextBrush, error) {
	fontFile, err := (FontFilePath)
	if err != nil {
		return nil, err
	}
	fontType, err := (fontFile)
	if err != nil {
		return nil, err
	}
	if textWidth <= 0 {
		textWidth = 20
	}
	return &TextBrush{FontType: fontType, FontSize: FontSize, FontColor: FontColor, TextWidth: textWidth}, nil
}

// Insert text in picturefunc (fb *TextBrush) DrawFontOnRGBA(rgba *, pt , content string) {
	c := ()
	(72)
	()
	()
	()
	(())
	(rgba)
	()
	(content, (, ))

}

func Image2RGBA(img ) * {

	baseSrcBounds := ().Max
	newWidth := 
	newHeight := 
	des := ((0, 0, newWidth, newHeight)) // Bottom plate	//First save an image information into jpg	(des, (), img, ().Min, )
	return des
}

Using example

func TestTextBrush_DrawFontOnRGBA(t *) {

	textBrush, err := NewTextBrush("Font library ttf location", 20, , 20)
	if err != nil {
		(err)
	}

	backgroud, err := GetImageFromFile("./resource/")
	if err != nil {
		(err)
	}
	des := Image2RGBA(backgroud)
	(des, (10, 50), "Hello the world")

	//Adjust the color	 = ({
		R: 0x8E,
		G: 0xE5,
		B: 0xEE,
		A: 255,
	})

	(des, (10, 80), "I'm using the text on Go")

	if err := SaveImage("./resource/", des); err != nil {
		(err)
	}
}

First use NewTextBrush The first parameter is the font library file location. The ttf format font library used here should have a free font library online.

Refer to the code in my example and you can use it directly.

Summarize

This is the article about Go's image splicing and text writing. For more relevant Go's image splicing and text writing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!