GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Python AI 绘画 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 Pycharm Python3 VScode Python3 基础语法 Python3 基本数据类型 Python3 数据类型转换 Python3 解释器 Python3 注释 Python3 运算符 Python3 数字(Number) Python3 字符串 Python3 列表 Python3 元组 Python3 字典 Python3 集合 Python3 条件控制 Python3 循环语句 Python3 编程第一步 Python3 推导式 Python3 迭代器与生成器 Python3 with Python3 函数 Python3 lambda Python3 装饰器 Python3 数据结构 Python3 模块 Python __name__ Python3 输入和输出 Python3 File Python3 OS Python3 错误和异常 Python3 面向对象 Python3 命名空间/作用域 Python 虚拟环境的创建 Python 类型注解 Python3 标准库概览 Python3 实例 Python 测验

Python3 高级教程

Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQt
Python 有用的资源
Python object() 函数

Python AI 绘画

本文我们将为大家介绍如何基于一些开发 API 或开源的库来搭建一套自己的 AI 作图工具。


通过文生图 API

通过文生图 API,您可以基于文本描述创造出全新的原创图像。

阿里云百炼提供两大系列模型:

  • 通义千问(Qwen-Image): 擅长渲染复杂的中英文文本,本章节以这个为例。
  • 通义万相(Wan系列): 用于生成写实图像和摄影级视觉效果。

通过运行以下命令安装 DashScope Python SDK:

# 如果运行失败,您可以将pip替换成pip3再运行
pip install -U requests dashscope

我们需要开通阿里云百炼模型服务并获得 API-KEY。

我们可以先使用阿里云主账号访问百炼模型服务平台:https://bailian.console.aliyun.com/,然后点击右上角登录,登录成功后点击右上角的齿轮⚙️图标,选择 API key,然后复制 API key,如果没有也可以创建 API key:

开通阿里云百炼不会产生费用,仅模型调用(超出免费额度后)、模型部署、模型调优会产生相应计费。

现在要使用 API,都需要按 token 来计费,还好都不贵,我们可以先购买个最便宜的包:阿里云百炼大模型服务平台。

接下来我们通过设置提示词来生成图片:

实例

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

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

# 请用百炼API Key
api_key = "sk-xxx"

print('----同步调用,请等待任务执行----')
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:
    # 在当前目录下保存图片
    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('同步调用失败, status_code: %s, code: %s, message: %s' %
          (rsp.status_code, rsp.code, rsp.message))

输出结果类似如下,会看到有个 url 的参数,我们访问它就可以下载通过提示词描述的图片了:

----同步调用,请等待任务执行----
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", 
...

以上实例生成的图片如下:

更多内容可以参考官方文档:https://help.aliyun.com/zh/model-studio/text-to-image


Stable Diffusion

需要使用的开源库为 Stable Diffusion web UI,它是基于 Gradio 库的 Stable Diffusion 浏览器界面

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

运行 Stable Diffusion 需要硬件要求比较高,运行时会消耗较大的资源,特别是显卡。

Windows 环境安装

本地环境要求安装 Python 3.10.6 或以上版本,并把它加入到本机的环境变量中。

下载 Stable Diffusion web UI GitHub 源码 https://github.com/AUTOMATIC1111/stable-diffusion-webui。

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

如果没有安装 Git ,可以在右上角下载 zip 压缩包。

解压 stable-diffusion-webui,并进入 stable-diffusion-webui 目录。

接下来我们需要下载模型,下载地址:https://huggingface.co/CompVis/stable-diffusion-v-1-4-original

将下载的模型移动到 stable-diffusion-webui/models/Stable-diffusion 目录下。

进入 stable-diffusion-webui 目录:

Windows 使用非管理员运行:

webui-user.bat

Linux 和 Mac OS 环境执行以下命令:

./webui.sh

接下来程序就会自动安装并启动,启动成功会看到一个可访问的 URL 地址 http://127.0.0.1:7860:

访问 http://127.0.0.1:7860,界面如下:

注意: 如果安装出现卡住不动,很可能是下载 Github 源码出现问题了,可以使用 Github 的一些镜像来解决,目前还没非常稳定的镜像,建议 Google 搜索下。 我这边在 2023 年 4 月 6 号使用以下镜像地址 https://hub.fgit.ml,打开 stable-diffusion-webui 目录的 launch.py 文件,替换以下部分代码的 Github 地址(代码大概在 230~240 行之间):

Civitai 介绍

Civitai 有许多定制好的模型,而且可以免费下载,我们使用国风3模型来测试,下载地址:https://civitai.com/models/10415/3-guofeng3?modelVersionId=36644

下载完后,我们将模型移动到 stable-diffusion-webui/models/Stable-diffusion 目录下,重新启动 stable-diffusion-webui :

./webui.sh

这样我们就可以在模型列表中选择国风3模型了:

选择后,我们可以到模型介绍页面去拷贝一些提示词和测试参数:

​为了快速生成,我高度跟宽度都减半了,之后点生成按钮即可:

生成完整过程,可以关注我们微信视频号查看:

AI 思考中...
Python 有用的资源
Python object() 函数

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.