国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python 使用 Stable Diffusion API 生成圖片示例

這篇具有很好參考價(jià)值的文章主要介紹了Python 使用 Stable Diffusion API 生成圖片示例。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Python 使用 Stable Diffusion API 生成圖片示例,Python,Stable Diffusion,python,stable diffusion
Python 使用 Stable Diffusion API 生成圖片示例,Python,Stable Diffusion,python,stable diffusion
代碼:

import base64
import datetime
import json
import os

import requests


def submit_post(url: str, data: dict):
    """
    Submit a POST request to the given URL with the given data.
    :param url:  url
    :param data: data
    :return:  response
    """
    return requests.post(url, data=json.dumps(data))


def save_encoded_image(b64_image: str, output_path: str):
    """
    Save the given image to the given output path.
    :param b64_image:  base64 encoded image
    :param output_path:  output path
    :return:  None
    """
    # 判斷當(dāng)前目錄下是否存在 output 文件夾,如果不存在則創(chuàng)建
    if not os.path.exists("output"):
        os.mkdir("output")
    timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    output_path = f"{output_path}_{timestamp}" + ".png"
    # 將文件放入當(dāng)前目錄下的 output 文件夾中
    output_path = f"output/{output_path}"
    with open(output_path, "wb") as f:
        f.write(base64.b64decode(b64_image))


def save_json_file(data: dict, output_path: str):
    """
    Save the given data to the given output path.
    :param data:  data
    :param output_path:  output path
    :return:  None
    """
    # 忽略 data 中的 images 字段
    data.pop('images')
    # 將 data 中的 info 字段轉(zhuǎn)為 json 字符串,info 當(dāng)前數(shù)據(jù)需要轉(zhuǎn)義
    data['info'] = json.loads(data['info'])

    # 輸出 data.info.infotexts
    info_texts = data['info']['infotexts']
    for info_text in info_texts:
        print(info_text)

    # 判斷當(dāng)前目錄下是否存在 output 文件夾,如果不存在則創(chuàng)建
    if not os.path.exists("output"):
        os.mkdir("output")
    timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    output_path = f"{output_path}_{timestamp}" + ".json"
    # 將文件放入當(dāng)前目錄下的 output 文件夾中
    output_path = f"output/{output_path}"
    with open(output_path, "w") as f:
        json.dump(data, f, indent=4, ensure_ascii=False)


if __name__ == '__main__':
    """
    Example usage: python3 txt2img.py
    """
    txt2img_url = "http://192.168.2.82:7860/sdapi/v1/txt2img" # 服務(wù)器地址
    prompt = input("請(qǐng)輸入提示詞:")
    negative_prompt = input("請(qǐng)輸入反面提示詞:")
    data = {'prompt': prompt, 'negative_prompt': negative_prompt}
    # 將 data.prompt 中的文本,刪除文件名非法字符,已下劃線分隔,作為文件名
    output_path = data['prompt'].replace(" ", "_").replace("/", "_").replace("\\", "_").replace(":", "_").replace("\"",
                                                                                                                  "_").replace(
        "<", "_").replace(">", "_").replace("|", "_")
    response = submit_post(txt2img_url, data)
    save_encoded_image(response.json()['images'][0], output_path)
    save_json_file(response.json(), output_path)

輸出:

>> python txt2img.py
請(qǐng)輸入提示詞:masterpiece, best quality, 1boy, orc, male focus, mecha, robot, science fiction, solo, black background <lora:nijiMecha:1> <lora:wzh-000022:1>
請(qǐng)輸入反面提示詞:(worst quality, low quality:1.4), greyscale, monochrome, watermark, signature

masterpiece, best quality, 1boy, orc, male focus, mecha, robot, science fiction, solo, black background <lora:nijiMecha:1> <lora:wzh-000022:1>
Negative prompt: (worst quality, low quality:1.4), greyscale, monochrome, watermark, signature
Steps: 50, Sampler: Euler, CFG scale: 7.0, Seed: 274772779, Size: 512x512, Model hash: ce67b54cf2, Model: oukaNiji5_v10, Seed resize from: -1x-1, Denoising strength: 0, Clip skip: 2


說明:

  • 運(yùn)行后,圖片以及 JSON 將會(huì)輸出到當(dāng)前目錄下 output 中;

TIP:文章來源地址http://www.zghlxwxcb.cn/news/detail-568059.html

  • 當(dāng)然前提是你已經(jīng)部署好 Stable Diffusion API 服務(wù);
  • 并且安裝好跟我一樣的模型以及一些相關(guān)的 LoRA;

print('HELLO WORLD') # http://www.skyner.cn

到了這里,關(guān)于Python 使用 Stable Diffusion API 生成圖片示例的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • AI圖片生成Stable Diffusion參數(shù)及使用方式詳細(xì)介紹

    AI圖片生成Stable Diffusion參數(shù)及使用方式詳細(xì)介紹

    ????????Stable Diffusion環(huán)境搭建與運(yùn)行請(qǐng)參考上一篇博文《AI圖片生成Stable Diffusion環(huán)境搭建與運(yùn)行》,地址為“https://blog.csdn.net/suiyingy/article/details/128896426”。運(yùn)行成功后,網(wǎng)頁瀏覽器顯示頁面主要包括txt2img、img2img、Extras、PNG Info、Checkpoint Merger、Train、Settings和Extensions等八

    2024年02月08日
    瀏覽(95)
  • 多模態(tài)——使用stable-video-diffusion將圖片生成視頻

    多模態(tài)——使用stable-video-diffusion將圖片生成視頻

    近期,stabilityAI發(fā)布了一個(gè)新的項(xiàng)目,是將圖片作為基礎(chǔ),生成一個(gè)相關(guān)的小視頻,其實(shí)也算是其之前研究內(nèi)容的擴(kuò)展。早在stable-diffusion的模型開源出來的時(shí)候,除了由prompt生成圖片之外,也可以生成連續(xù)幀的短視頻。 本文主要是體驗(yàn)一下stable-video-diffusion的使用,以及對(duì)其

    2024年02月05日
    瀏覽(126)
  • 圖生圖—AI圖片生成Stable Diffusion參數(shù)及使用方式詳細(xì)介紹

    圖生圖—AI圖片生成Stable Diffusion參數(shù)及使用方式詳細(xì)介紹

    ????????本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 ????????本文為專欄《Python從零開始進(jìn)行AIGC大模型訓(xùn)練與推理》系列文章,地址為“https://blog.csdn.net/suiyingy/article/details/130169592”。 ????????Stable Diffusion webui的詳細(xì)安裝步驟以及文生圖(txt2img)功能詳細(xì)介紹

    2024年02月08日
    瀏覽(93)
  • 阿里云免費(fèi)使用stable diffusion三個(gè)月【ai生成圖片】詳細(xì)教程【保姆級(jí)】

    阿里云免費(fèi)使用stable diffusion三個(gè)月【ai生成圖片】詳細(xì)教程【保姆級(jí)】

    這兩天關(guān)注了ai生成圖片,嘗試了mijiourney服務(wù)【比較貴沒入手】,結(jié)果免費(fèi)的沒有了,沒用上,換了國內(nèi)的一些小程序體驗(yàn)了下 綜合體驗(yàn)式是太慢了,而他們是基于國外開源的stable diffiusion模型開發(fā)的【可以比肩mijiourney】,所以我動(dòng)了開發(fā)一個(gè)自己用的念頭 不會(huì)的同學(xué)可以

    2024年02月05日
    瀏覽(433)
  • Stable Diffusion 圖片換臉插件Roop保姆教程 附錯(cuò)誤解決辦法和API使用

    Stable Diffusion 圖片換臉插件Roop保姆教程 附錯(cuò)誤解決辦法和API使用

    換臉技術(shù)已經(jīng)不是新鮮事物,但如何實(shí)現(xiàn)簡單、快速、高效的換臉操作呢?Roop插件正是為解決這一問題而生的。 sd-webui-roop 插件適用于已經(jīng)本地部署了SD的用戶。相較于傳統(tǒng)的換臉技術(shù),Roop插件幾乎不需要訓(xùn)練,只需一張照片,即可在10秒內(nèi)完成換臉。 但是要注意到是必須注

    2024年02月08日
    瀏覽(28)
  • 使用Unity 接入 Stable-Diffusion-WebUI的 文生圖api 并生成圖像

    使用Unity 接入 Stable-Diffusion-WebUI的 文生圖api 并生成圖像

    在無聊的時(shí)候,想瞅一下sd生圖遂做了一下 注意:我采用了異步處理,每點(diǎn)擊一次發(fā)送一次請(qǐng)求,不需要等待生成完再點(diǎn)擊。后面生成的畫面會(huì)覆蓋掉之前的。 1、啟動(dòng)SD的api設(shè)置 注意,運(yùn)行后的api相關(guān)功能可以在:http://127.0.0.1:7860/docs 查看 比如這一次我們要的生圖的地址就是

    2024年04月23日
    瀏覽(93)
  • 【AIGC】如何在使用stable-diffusion-webui生成圖片時(shí)看到完整請(qǐng)求參數(shù)

    【AIGC】如何在使用stable-diffusion-webui生成圖片時(shí)看到完整請(qǐng)求參數(shù)

    通過代碼調(diào)用Stable Diffusion的txt2img、img2img接口時(shí),很多時(shí)候都不知道應(yīng)該怎么傳參,比如如何指定模型、如何開啟并使用Controlnet、如何開啟面部修復(fù)等等,在sd-webui上F12看到的請(qǐng)求也不是正式調(diào)用SD的請(qǐng)求,所以當(dāng)引入新插件或需要使用新功能時(shí),怎么傳參成了一個(gè)大問題,

    2024年02月08日
    瀏覽(28)
  • stable diffusion API 調(diào)用,超級(jí)詳細(xì)代碼示例和說明

    本文主要介紹 stable diffusion API 調(diào)用,準(zhǔn)確來說是對(duì) stable diffusion webui 的 API 調(diào)用。接口文檔可以查看: http://sd-webui.test.cn/docs 這里的 sd-webui.test.cn 是自己的 sd webui Endpoint。 文生圖是: /sdapi/v1/txt2img 這個(gè) POST 接口。 圖生圖是: /sdapi/v1/img2img 這個(gè) POST 接口。 本文主要介紹文生

    2024年02月12日
    瀏覽(30)
  • Stable Diffusion生成圖片

    Stable Diffusion生成圖片

    畫質(zhì) masterpiece,best quality,illustration,extremely detail CG unity 8k wallpaper,ultra-detailed,depth of field 杰作,最佳質(zhì)量,插圖,極度詳細(xì)的8K壁紙,超高詳細(xì)度,景深 畫風(fēng) Chinese ink painting,water color,oil painting,sketch,realistic 中國水墨畫,水彩,油畫,素描,寫實(shí) 人物 1girl,solo,upper body,black hair,

    2024年02月07日
    瀏覽(19)
  • Stable Diffusion 生成高清圖片

    Stable Diffusion 生成高清圖片

    記錄一下使用 Stable Diffusion 生成高清圖片使用的插件和過程。 打開 Stable Diffusion,單擊 “擴(kuò)展” = “從網(wǎng)址安裝”, 輸入下面項(xiàng)目,單擊 “安裝”, 擴(kuò)展的 git 倉庫網(wǎng)址: https://github.com/Coyote-A/ultimate-upscale-for-automatic1111 然后單擊 “已安裝\\\",確認(rèn) “ultimate-upscale-for-automatic1

    2024年02月08日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包