I want to add a text on an given co-ordinates of an image using the gocv library (also the text should expand from the co-ordinates; not from left to right).
Using the below command I was able to achieve both requirements. How can I load a custom .ttf font file and do the same thing ?
import (
"fmt"
"image"
"image/color"
"os"
"github.com/gofiber/fiber/v2"
"gocv.io/x/gocv"
)
func GeneralRoutes(gen fiber.Router) {
gen.Get("/image", func(c *fiber.Ctx) error {
imageFile := "path/input.png"
outputFile := "path/output.jpg"
img := gocv.IMRead(imageFile, gocv.IMReadColor)
if img.Empty() {
fmt.Printf("Error opening image: %s\n", imageFile)
os.Exit(1)
}
// Create a font type and scale
font := gocv.FontHersheyPlain
scale := 5.5
thickness := 5
// Define the text to be added, position, font type and color
text := "Hello World"
textSize := gocv.GetTextSize(text, font, scale, thickness)
textPos := img.Point{X: 1170 - textSize.X/2, Y: 663 - textSize.Y/2}
gocv.PutText(&img, text, textPos, font, scale, color.RGBA{0, 0, 0, 0}, thickness)
// Save the output image
gocv.IMWrite(outputFile, img)
return c.SendFile(outputFile)
})
}