Python实现一行代码自动绘制艺术画
作者:Ckend 发布时间:2021-03-26 00:32:54
DiscoArt 是一个很牛的开源模块,它能根据你给定的关键词自动绘画。
绘制过程是完全可见的,你可以在 jupyter 页面上看见这个绘制的过程:
1.准备
开始之前,你要确保Python和pip已经成功安装在电脑上,如果没有,可以访问这篇文章:超详细Python安装指南 进行安装。
(可选1) 如果你用Python的目的是数据分析,可以直接安装Anaconda,它内置了Python和pip.
(可选2) 此外,推荐大家用VSCode编辑器,它有许多的优点
请选择以下任一种方式输入命令安装依赖:
1. Windows 环境 打开 Cmd (开始-运行-CMD)。
2. MacOS 环境 打开 Terminal (command+空格输入Terminal)。
3. 如果你用的是 VSCode编辑器 或 Pycharm,可以直接使用界面下方的Terminal.
pip install discoart
为了运行 Discoart, 你需要Python 3.7+ 和支持 CUDA 的 PyTorch.
2.开始使用 Discoart
你可以在Jupyter中运行Discoart,这样能方便地实时展示绘制过程:
from discoart import create
da = create()
这样将使用默认的 文本描述 和参数创建图像:
text_prompts:
- A beautiful painting of a singular lighthouse, shining its light across a tumultuous sea of blood by greg rutkowski and thomas kinkade, Trending on artstation.
- yellow color scheme
init_image:
width_height: [ 1280, 768 ]
skip_steps: 0
steps: 250
init_scale: 1000
clip_guidance_scale: 5000
tv_scale: 0
range_scale: 150
sat_scale: 0
cutn_batches: 4
diffusion_model: 512x512_diffusion_uncond_finetune_008100
use_secondary_model: True
diffusion_sampling_mode: ddim
perlin_init: False
perlin_mode: mixed
seed:
eta: 0.8
clamp_grad: True
clamp_max: 0.05
randomize_class: True
clip_denoised: False
rand_mag: 0.05
cut_overview: "[12]*400+[4]*600"
cut_innercut: "[4]*400+[12]*600"
cut_icgray_p: "[0.2]*400+[0]*600"
cut_ic_pow: 1.
save_rate: 20
gif_fps: 20
gif_size_ratio: 0.5
n_batches: 4
batch_size: 1
batch_name:
clip_models:
- ViT-B-32::openai
- ViT-B-16::openai
- RN50::openai
clip_models_schedules:
use_vertical_symmetry: False
use_horizontal_symmetry: False
transformation_percent: [0.09]
on_misspelled_token: ignore
diffusion_model_config:
cut_schedules_group:
name_docarray:
skip_event:
stop_event:
text_clip_on_cpu: False
truncate_overlength_prompt: False
image_output: True
visualize_cuts: False
display_rate: 1
创建出来的就是这个图:
Create 支持的所有参数如下:
text_prompts:
- A beautiful painting of a singular lighthouse, shining its light across a tumultuous sea of blood by greg rutkowski and thomas kinkade, Trending on artstation.
- yellow color scheme
init_image:
width_height: [ 1280, 768 ]
skip_steps: 0
steps: 250
init_scale: 1000
clip_guidance_scale: 5000
tv_scale: 0
range_scale: 150
sat_scale: 0
cutn_batches: 4
diffusion_model: 512x512_diffusion_uncond_finetune_008100
use_secondary_model: True
diffusion_sampling_mode: ddim
perlin_init: False
perlin_mode: mixed
seed:
eta: 0.8
clamp_grad: True
clamp_max: 0.05
randomize_class: True
clip_denoised: False
rand_mag: 0.05
cut_overview: "[12]*400+[4]*600"
cut_innercut: "[4]*400+[12]*600"
cut_icgray_p: "[0.2]*400+[0]*600"
cut_ic_pow: 1.
save_rate: 20
gif_fps: 20
gif_size_ratio: 0.5
n_batches: 4
batch_size: 1
batch_name:
clip_models:
- ViT-B-32::openai
- ViT-B-16::openai
- RN50::openai
clip_models_schedules:
use_vertical_symmetry: False
use_horizontal_symmetry: False
transformation_percent: [0.09]
on_misspelled_token: ignore
diffusion_model_config:
cut_schedules_group:
name_docarray:
skip_event:
stop_event:
text_clip_on_cpu: False
truncate_overlength_prompt: False
image_output: True
visualize_cuts: False
display_rate: 1
你可以这么使用参数:
from discoart import create
da = create(
text_prompts='A painting of sea cliffs in a tumultuous storm, Trending on ArtStation.',
init_image='https://d2vyhzeko0lke5.cloudfront.net/2f4f6dfa5a05e078469ebe57e77b72f0.png',
skip_steps=100,
)
如果你不是用jupyter运行的,你也可以看到中间结果,因为最终结果和中间结果都会被创建在当前工作目录下,即
./{name-docarray}/{i}-done.png
./{name-docarray}/{i}-step-{j}.png
./{name-docarray}/{i}-progress.png
./{name-docarray}/{i}-progress.gif
./{name-docarray}/da.protobuf.lz4
name-docarray
是运行时定义的名称,如果没有定义,则会随机生成。i-*
第几个Batch。*-done-*
是当前Batch完成后的最终图像。*-step-*
是某一步的中间图像,实时更新。*-progress.png
是到目前为止所有中间结果的png图像,实时更新。*-progress.gif
是到目前为止所有中间结果的动画 gif,实时更新。da.protobuf.lz4
是到目前为止所有中间结果的压缩 protobuf,实时更新。
3.显示/保存/加载配置
如果你想知道你当前绘图的配置,有三种方法:
from discoart import show_config
show_config(da) # show the config of the first run
show_config(da[3]) # show the config of the fourth run
show_config(
'discoart-06030a0198843332edc554ffebfbf288'
) # show the config of the run with a known DocArray ID
要保存 Document/DocumentArray 的配置:
from discoart import save_config
save_config(da, 'my.yml') # save the config of the first run
save_config(da[3], 'my.yml') # save the config of the fourth run
从配置中导入:
from discoart import create, load_config
config = load_config('my.yml')
create(**config)
此外,你还能直接把配置导出为图像的形式
from discoart.config import save_config_svg
save_config_svg(da)
来源:https://mp.weixin.qq.com/s/pzUZqm5ZReDiCOROHysncQ
猜你喜欢
- int connectDb() { EXEC SQL BEGIN DECLARE SECTION; char username[20]; c
- SQL Server 平台修改自增列值由于之前处理过sql server数据库的迁移工作,尝试过其自增列值的变更,但是通过SQL 语句修改自
- 本文实例讲述了Python实现的微信公众号群发图片与文本消息功能。分享给大家供大家参考,具体如下:在微信公众号开发中,使用api都要附加ac
- 前言order by满足两种情况,会使用 index 方式排序:order by语句使用索引最左前列(最左匹配法则)where子句和orde
- 介绍:今天在使用django的时候忽然想用到,如何匹配多个关键字的操作,我们知道django有一个objects.filter()方法,我们
- python3 shelve模块的详解一、简介在python3中我们使用json或者pickle持久化数据,能dump多次,但只能load一
- 本文实例讲述了MySQL 的启动选项和系统变量。分享给大家供大家参考,具体如下:MySQL的配置信息可以通过两种方式实现,一种是命令行形式,
- 本文实例讲述了python基于queue和threading实现多线程下载的方法,分享给大家供大家参考。具体方法如下:主代码如下: &nbs
- 前言一直都觉得vue的插件生涩难懂,但是又很好奇,在看了几篇文章,试着写了写之后觉得也没那么难,本文主要实现一个简单的Toast插件,方便迁
- 1.简介: java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包。 它包括两个类:Pattern和Ma
- 公式首先需要了解CrossEntropyLoss的计算过程,交叉熵的函数是这样的:其中,其中yi表示真实的分类结果。这里只给出公式,关于Cr
- 一、configparser模块是什么可以用来操作后缀为 .ini 的配置文件;python标准库(就是python自带的意思,无需安装)二
- 功能:返回字符、二进制、文本或图像表达式的一部分语法:SUBSTRING ( expression, start, length ) 1、s
- 这篇文章主要介绍了python接口自动化如何封装获取常量的类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 1.首先在项目中添加必备js与css 2.代码中添加引用(必备引用) <script src="Scripts/
- 如何对设计进行评判,一定有很多答案,有利有弊。问100位设计师,会得到100种回答。用线上PV、UV等数据说话,更多受产品属性、运营动作影响
- 1、在外部函数中定义内部函数,内部函数包含访问外部函数。即使外部函数的生命周期结束后,内部函数仍然可以访问外部函数变量。2、外部函数的返回值
- pandas有groupby分组函数和sort_values排序函数,但是如何对dataframe分组之后排序呢?In [70]: df =
- 直接切入主题,从HTML页面上传文件,Python接收处理。但其中发现有些小问题,把它写出来,算是积累吧!HTML页面代码:<form
- 要处理文本数据,需要比数字类型的数据更多的清理步骤。为了从文本数据中提取有用和信息,通常需要执行几个预处理和过滤步骤。Pandas 库有许多