Artificial Intelligence 15 min read

Building an AI‑Powered Image Generation Mini‑Program with Go Backend and Tencent Cloud

The article walks through building a WeChat mini‑program that turns user‑typed text into cartoon‑style images by using Go to query Sogou’s picture search API, passing the first result to Tencent Cloud’s FaceCartoonPic service, and exposing the workflow through a simple HTTP endpoint.

Tencent Cloud Developer
Tencent Cloud Developer
Tencent Cloud Developer
Building an AI‑Powered Image Generation Mini‑Program with Go Backend and Tencent Cloud

This article describes how to create a mini‑program that generates images from user‑provided text using AI. The overall workflow consists of three parts: (1) text‑to‑image search, (2) image style‑transfer, and (3) a simple HTTP service that ties the two together.

Text‑to‑image search – The author uses Sogou’s picture search API. By sending a keyword (and optional tag filters) to https://pic.sogou.com/napi/pc/searchList the API returns a list of image URLs. The Go implementation of the request is shown below:

params := url.Values{}
params.Set("mode", "1")
params.Set("start", "0")
params.Set("xml_len", "48")
params.Set("query", keywords)
// optional tag filter handling omitted for brevity
uri := "https://pic.sogou.com/napi/pc/searchList"
address, _ := url.Parse(uri)
address.RawQuery = params.Encode()
request, _ := http.NewRequestWithContext(ctx, http.MethodGet, address.String(), nil)
resp, _ := http.DefaultClient.Do(request)
body, _ := ioutil.ReadAll(resp.Body)
var rsp Response
json.Unmarshal(body, &rsp)

The response structure is defined in Go (see the long type Response struct { … } in the source).

Image style‑transfer – After obtaining a suitable picture, the author calls Tencent Cloud’s FaceCartoonPic API to convert the image into a cartoon style. The required SDK calls are:

credential := common.NewCredential("***", "***")
cpf := profile.NewClientProfile()
client, _ := ft.NewClient(credential, "ap-guangzhou", cpf)
req := ft.NewFaceCartoonPicRequest()
req.Url = common.StringPtr(imageUrl)
req.RspImgType = common.StringPtr("url")
resp, err := client.FaceCartoonPic(req)
if err != nil { /* handle error */ }
return []byte(*resp.Response.ResultUrl), nil

Both the request/response protobuf definitions and the Go wrapper functions ( FaceCartoonPicPro , SearchImage ) are provided.

HTTP service – The backend exposes a single endpoint /SearchImage that accepts a JSON payload containing the text and optional tag JSON. It performs the search, calls the style‑transfer API on the first successful image, and returns a list of original and result URLs.

http.HandleFunc("/SearchImage", func(w http.ResponseWriter, r *http.Request) {
    data, _ := ioutil.ReadAll(r.Body)
    req := &pb.SearchImageReq{}
    json.Unmarshal(data, &req)
    rsp := &pb.SearchImageRsp{}
    SearchImage(context.Background(), req, rsp)
    out, _ := json.Marshal(rsp)
    w.Write(out)
})
http.ListenAndServe("127.0.0.1:8080", nil)

WeChat mini‑program front‑end – The UI consists of a simple input box, a button, and a canvas to display the generated image. Key files are:

WXML (layout):

<view class="container">
  <div class="form-item" style="width:673rpx;height:70rpx;">
    <input placeholder="写下你的创意" bindinput="handlerInput" />
    <button loading="{{buttonStatus}}" bindtap="handlerSearch">立即生成</button>
  </div>
  ...
  <canvas type="2d" id="input_canvas" style="background:#e4e4e1;width:673rpx;height:700rpx;"></canvas>
</view>

JS (logic):

Page({
  data:{ inputValue:"", tags:[], option:[], buttonStatus:false },
  handlerInput(e){ this.setData({inputValue:e.detail.value}) },
  handlerSearch(){ if(this.data.inputValue.length===0){wx.showToast({icon:"error",title:"请输入你的创意"});return;} this.imageDraw(); },
  imageDraw(){
    const opt = this.data.option.length?{tags:this.data.option}:{};
    wx.request({
      url:'http://127.0.0.1:8080/SearchImage',
      method:'POST',
      data:{text:this.data.inputValue, option_json:JSON.stringify(opt)},
      success:res=>{ /* parse response, download result image, draw on canvas */ }
    })
  }
})

The demo shows successful generation of cartoon‑style images for several sample keywords, with screenshots included in the original article.

In conclusion, the author demonstrates a complete end‑to‑end solution for AI‑driven image creation: a searchable image repository, a cloud‑based style‑transfer service, a Go backend that orchestrates the workflow, and a WeChat mini‑program front‑end for user interaction.

WeChat Mini ProgramAI image generationTencent CloudGo backendimage style transferSogou API
Tencent Cloud Developer
Written by

Tencent Cloud Developer

Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.