Python實(shí)現(xiàn)一行代碼自動(dòng)繪制藝術(shù)畫(huà)
DiscoArt 是一個(gè)很牛的開(kāi)源模塊,它能根據(jù)你給定的關(guān)鍵詞自動(dòng)繪畫(huà)。
繪制過(guò)程是完全可見(jiàn)的,你可以在 jupyter 頁(yè)面上看見(jiàn)這個(gè)繪制的過(guò)程:

1.準(zhǔn)備
開(kāi)始之前,你要確保Python和pip已經(jīng)成功安裝在電腦上,如果沒(méi)有,可以訪問(wèn)這篇文章:超詳細(xì)Python安裝指南 進(jìn)行安裝。
(可選1) 如果你用Python的目的是數(shù)據(jù)分析,可以直接安裝Anaconda,它內(nèi)置了Python和pip.
(可選2) 此外,推薦大家用VSCode編輯器,它有許多的優(yōu)點(diǎn)
請(qǐng)選擇以下任一種方式輸入命令安裝依賴:
1. Windows 環(huán)境 打開(kāi) Cmd (開(kāi)始-運(yùn)行-CMD)。
2. MacOS 環(huán)境 打開(kāi) Terminal (command+空格輸入Terminal)。
3. 如果你用的是 VSCode編輯器 或 Pycharm,可以直接使用界面下方的Terminal.
pip?install?discoart
為了運(yùn)行 Discoart, 你需要Python 3.7+ 和支持 CUDA 的 PyTorch.
2.開(kāi)始使用 Discoart
你可以在Jupyter中運(yùn)行Discoart,這樣能方便地實(shí)時(shí)展示繪制過(guò)程:
from?discoart?import?create da = create()
這樣將使用默認(rèn)的 文本描述 和參數(shù)創(chuàng)建圖像:
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
創(chuàng)建出來(lái)的就是這個(gè)圖:

Create 支持的所有參數(shù)如下:
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
你可以這么使用參數(shù):
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運(yùn)行的,你也可以看到中間結(jié)果,因?yàn)樽罱K結(jié)果和中間結(jié)果都會(huì)被創(chuàng)建在當(dāng)前工作目錄下,即
./{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是運(yùn)行時(shí)定義的名稱,如果沒(méi)有定義,則會(huì)隨機(jī)生成。i-*第幾個(gè)Batch。*-done-*是當(dāng)前Batch完成后的最終圖像。*-step-*是某一步的中間圖像,實(shí)時(shí)更新。*-progress.png是到目前為止所有中間結(jié)果的png圖像,實(shí)時(shí)更新。*-progress.gif是到目前為止所有中間結(jié)果的動(dòng)畫(huà) gif,實(shí)時(shí)更新。da.protobuf.lz4是到目前為止所有中間結(jié)果的壓縮 protobuf,實(shí)時(shí)更新。
3.顯示/保存/加載配置
如果你想知道你當(dāng)前繪圖的配置,有三種方法:
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
從配置中導(dǎo)入:
from?discoart?import?create, load_config
config = load_config('my.yml')
create(**config)此外,你還能直接把配置導(dǎo)出為圖像的形式
from?discoart.config?import?save_config_svg save_config_svg(da)

到此這篇關(guān)于Python實(shí)現(xiàn)一行代碼自動(dòng)繪制藝術(shù)畫(huà)的文章就介紹到這了,更多相關(guān)Python繪制藝術(shù)畫(huà)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python編碼時(shí)應(yīng)該注意的幾個(gè)情況
對(duì)于Python程序員,你需要注意一下本文所提到的這些事情。你也可以看看Zen of Python(Python之禪),這里面提到了一些注意事項(xiàng),并配以示例,可以幫助你快速提高2013-03-03
三個(gè)Python自動(dòng)化辦公好用到爆的模塊分享
本文小編來(lái)給大家推薦幾個(gè)在自動(dòng)化辦公領(lǐng)域當(dāng)中非常好用的Python模塊,可以幫助大家在工作當(dāng)中及大地提高效率,避免重復(fù)機(jī)械化地操作流程2022-07-07
使用Django xadmin 實(shí)現(xiàn)修改時(shí)間選擇器為不可輸入狀態(tài)
這篇文章主要介紹了使用Django xadmin 實(shí)現(xiàn)修改時(shí)間選擇器為不可輸入狀態(tài),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
pytorch實(shí)現(xiàn)保證每次運(yùn)行使用的隨機(jī)數(shù)都相同
今天小編就為大家分享一篇pytorch實(shí)現(xiàn)保證每次運(yùn)行使用的隨機(jī)數(shù)都相同,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Pytest初學(xué)者快速上手高效Python測(cè)試指南
這篇文章主要為大家介紹了Pytest初學(xué)者快速上手的高效Python測(cè)試指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
python讀取excel指定列數(shù)據(jù)并寫(xiě)入到新的excel方法
今天小編就為大家分享一篇python讀取excel指定列數(shù)據(jù)并寫(xiě)入到新的excel方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07

