YouTip LogoYouTip

Python Ai Draw

## Python3.x Python AI Painting In this article, we will introduce how to build your own AI drawing tool based on some development APIs or open-source libraries. * * * ## Text-to-Image API Through the Text-to-Image API, you can create brand new original images based on text descriptions. Alibaba Cloud Bailian provides two major series of models: * **Qwen-Image**: Excels at rendering complex Chinese and English text, this chapter uses this as an example. * **Wan Series**: Used for generating realistic images and photographic-level visual effects. Install the DashScope Python SDK by running the following command: # If it fails, you can replace pip with pip3 and run: pip install -U requests dashscope We need to activate the Alibaba Cloud Bailian model service and obtain an API-KEY. We can first use the Alibaba Cloud main account to access the Bailian model service platform: [https://bailian.console.aliyun.com/](https://bailian.console.aliyun.com/), then click the login button in the top right corner. After successful login, click the gear βš™οΈ icon in the top right corner, select API key, and copy the API key. If there isn't one, you can also create an API key: !(#) !(#) Activating Alibaba Cloud Bailian will not incur any fees. Only model calls (after exceeding the free quota), model deployment, and model tuning will generate corresponding charges. Now to use the API, all calls are charged by token. Fortunately, they are not expensive. We can first purchase the cheapest package: (https://www.aliyun.com/minisite/goods?userCode=i5mn5r7m). [!(#)](https://www.aliyun.com/minisite/goods?userCode=i5mn5r7m) Next, we generate images by setting prompts: ## Example from http import HTTPStatus from urllib.parse import urlparse, unquote from pathlib import PurePosixPath import requests from dashscope import ImageSynthesis import os prompt ="An elegant and solemn couplet hangs in the hall, the room features a quiet, classical Chinese decor, with some blue and white porcelain on the table, the left side of the couplet reads"Righteousness roots in knowledge, humans and machines share the same path, embracing thoughtful innovation",written on the right"Tongyun bestows wisdom, the universe unlocks data, high aspirations reach far", Horizontal banner"Wisdom Inspires Tongyi",The calligraphy is elegant and flowing, and in the center hangs a Chinese-style painting depicting Yueyang Tower." # Please use the Bailian API Key api_key ="sk-xxx" print('----Synchronous 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 with open('./%s' % file_name,'wb+')as f: f.write(requests.get(result.url).content) else: print('Synchronous call failed, status_code: %s, code: %s, message: %s' % (rsp.status_code, rsp.code, rsp.message)) The output result is similar to below. You will see a url parameter. We can access it to download the image described by the prompt: ----Synchronous 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/8d
← Matplotlib HistJsref Flat Array β†’