Draw Text Onto an Image in Go
1 min readMar 18, 2022
With a single Cloudmersive API key, you can easily implement dozens of our Image Processing & editing operations into your project. Below, ready-to-run code is available to implement the text drawing iteration of our Image API, which will enable you to generate a customized text visual on an image with specific parameters including font, font size, color, and location on the image (using an X/Y axis of pixels).
Node.JS Python C# Java PHP Objective-C Ruby Apex C/C++ cURL Swift JavaScript Go
package mainimport (
"fmt"
"strings"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/image/edit/draw/text"
method := "POST"payload := strings.NewReader("BaseImageBytes=%3Cbyte%3E&BaseImageUrl=%3Cstring%3E&TextToDraw=%7B%22Text%22%3A%22%3Cstring%3E%22%2C%22FontFamilyName%22%3A%22%3Cstring%3E%22%2C%22FontSize%22%3A%22%3Cdouble%3E%22%2C%22Color%22%3A%22%3Cstring%3E%22%2C%22X%22%3A%22%3Cdouble%3E%22%2C%22Y%22%3A%22%3Cdouble%3E%22%2C%22Width%22%3A%22%3Cdouble%3E%22%2C%22Height%22%3A%22%3Cdouble%3E%22%7D&TextToDraw=%7B%22Text%22%3A%22%3Cstring%3E%22%2C%22FontFamilyName%22%3A%22%3Cstring%3E%22%2C%22FontSize%22%3A%22%3Cdouble%3E%22%2C%22Color%22%3A%22%3Cstring%3E%22%2C%22X%22%3A%22%3Cdouble%3E%22%2C%22Y%22%3A%22%3Cdouble%3E%22%2C%22Width%22%3A%22%3Cdouble%3E%22%2C%22Height%22%3A%22%3Cdouble%3E%22%7D")client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
All you need to do is copy & paste the above code into your project, and include your image file & Cloudmersive API key.