Skip to content

Python AI Drawing

This article introduces how to build your own AI drawing tool based on development APIs or open-source libraries.


With the Text-to-Image API, you can create entirely new original images based on text descriptions.

Alibaba Cloud Bailian provides two major model series:

  • Qwen-Image: Excels at rendering complex Chinese and English text. This chapter uses this as an example.
  • Tongyi Wanxiang (Wan series): Used for generating realistic images and photographic-level visual effects.

Install the DashScope Python SDK by running the following command:

# If the installation fails, you can replace pip with pip3 and try again
pip install -U requests dashscope

You need to activate the Alibaba Cloud Bailian model service and obtain an API-KEY.

First, visit the Bailian model service platform using your Alibaba Cloud primary account: https://bailian.console.aliyun.com/, then click the login button in the upper right corner. After logging in, click the gear ⚙️ icon in the upper right corner, select API key, and then copy the API key. If you don’t have one, you can create an API key:

Activating Alibaba Cloud Bailian does not incur charges. Only model calls (beyond the free quota), model deployment, and model fine-tuning will generate corresponding billing.

Now, to use the API, you need to pay by token. Fortunately, it’s not expensive. You can purchase the cheapest package first: Alibaba Cloud Bailian Large Model Service Platform.

Next, we generate images by setting prompts:

from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os

prompt = "一副典雅庄重的对联悬挂于厅堂之中,房间是个安静古典的中式布置,桌子上放着一些青花瓷,对联上左书"义本生知人机同道善思新",右书"通云赋智乾坤启数高志远", 横批"智启通义",字体飘逸,中间挂在一着一副中国风的画作,内容是岳阳楼。"

# Please use your Bailian API Key
api_key = "sk-xxx"

print('----Sync call, please wait for task execution----')
rsp = ImageSynthesis.call(api_key=api_key,
                          model="qwen-image",
                          prompt=prompt,
                          n=1,
                          size='1328*1328',
                          prompt_extend=True,
                          watermark=True)
print('response: %s' % rsp)
if rsp.status_code == HTTPStatus.OK:
    # Save the image in the current directory
    for result in rsp.output.results:
        file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
        with open('./%s' % file_name, 'wb+') as f:
            f.write(requests.get(result.url).content)
else:
    print('Sync call failed, status_code: %s, code: %s, message: %s' %
          (rsp.status_code, rsp.code, rsp.message))

The output result will be similar to the following. You will see a url parameter; visiting it allows you to download the image described by the prompt:

----Sync call, please wait for task execution----
response: {"status_code": 200, "request_id": "1968746e-f434-4f77-9f8c-9b1adb80e8d7", "code": null, "message": "", "output": {"task_id": "d85c65d9-c4ff-4d66-a895-7c7746620b0c", "task_status": "SUCCEEDED", "results": [{"url": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/7d/05/20250916/8d68e658/d85c65d9-c4ff-4d66-a895-7c7746620b0c-1.png?xxxxxxxxx", 
...

The image generated by the above example is as follows:

For more information, refer to the official documentation: https://help.aliyun.com/zh/model-studio/text-to-image


The open-source library needed is Stable Diffusion web UI, which is a browser interface for Stable Diffusion based on the Gradio library.

Stable Diffusion web UI GitHub address: https://github.com/AUTOMATIC1111/stable-diffusion-webui

Running Stable Diffusion requires relatively high hardware requirements and consumes significant resources, especially the graphics card.

The local environment requires Python 3.10.6 or above, and it must be added to the system environment variables.

Download the Stable Diffusion web UI source code from GitHub: https://github.com/AUTOMATIC1111/stable-diffusion-webui.

git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git

If Git is not installed, you can download the zip archive from the upper right corner.

Extract stable-diffusion-webui and enter the stable-diffusion-webui directory.

Next, we need to download the model from: https://huggingface.co/CompVis/stable-diffusion-v-1-4-original

Move the downloaded model to the stable-diffusion-webui/models/Stable-diffusion directory.

Enter the stable-diffusion-webui directory:

For Windows, run as non-administrator:

webui-user.bat

For Linux and Mac OS environments, execute the following command:

./webui.sh

The program will then automatically install and start. Upon successful startup, you will see an accessible URL address http://127.0.0.1:7860:

Visit http://127.0.0.1:7860, and the interface is as follows:

Note: If the installation gets stuck, it’s likely due to problems downloading Github source code. You can use some Github mirrors to resolve this. Currently, there are no very stable mirrors; it’s recommended to search on Google. I used the following mirror address https://hub.fgit.ml on April 6, 2023. Open the launch.py file in the stable-diffusion-webui directory and replace the Github address in the following part of the code (around lines 230-240):

Civitai has many customized models that can be downloaded for free. We’ll use the Guofeng3 model for testing. Download address: https://civitai.com/models/10415/3-guofeng3?modelVersionId=36644

After downloading, move the model to the stable-diffusion-webui/models/Stable-diffusion directory and restart stable-diffusion-webui:

./webui.sh

This way, you can select the Guofeng3 model from the model list:

After selecting, you can go to the model introduction page to copy some prompts and test parameters:

To generate quickly, I halved both the height and width, then clicked the generate button:

For the complete generation process, you can follow our WeChat video channel to view: