首页 > 其他分享 >建筑转手稿,调完提示词

建筑转手稿,调完提示词

时间:2024-03-29 14:23:48浏览次数:14  
标签:name 调完 提示 os image base file path 手稿

import requests
import base64
import os
from PIL import Image
import io
import random

def encode_pil_to_base64(image):
    """
    Encode a PIL image to a Base64 string.
    """
    with io.BytesIO() as output_bytes:
        image.save(output_bytes, format="PNG")  # Using PNG format
        bytes_data = output_bytes.getvalue()
    return base64.b64encode(bytes_data).decode("utf-8")

def save_decoded_image(b64_image, base_path):
    """
    Save a Base64 encoded image to a file.
    Automatically adjusts the filename by adding a sequence number
    if a file with the same name already exists.
    """
    # Initialize the sequence number and the file path
    seq = 0
    output_path = base_path

    # Check if the file exists and adjust the filename if necessary
    while os.path.exists(output_path):
        seq += 1
        # Split the base path into the directory, basename, and extension
        dir_name, base_name = os.path.split(base_path)
        name, ext = os.path.splitext(base_name)
        # Construct a new filename with the sequence number
        output_path = os.path.join(dir_name, f"{name}({seq}){ext}")

    # Save the image to the new path
    with open(output_path, 'wb') as image_file:
        image_file.write(base64.b64decode(b64_image))
    print(f"Image saved to: {output_path}")

def main():
    # API URL
    url = "http://127.0.0.1:7860/sdapi/v1/txt2img"  # Update to img2img API if needed

    # Path to the folder containing reference images
    reference_image_dir = r"C:\Users\wujie1\Desktop\图片参考素材"

    # Find the latest file in the reference image folder
    latest_file_path = max(
        [os.path.join(reference_image_dir, f) for f in os.listdir(reference_image_dir) if os.path.isfile(os.path.join(reference_image_dir, f))],
        key=os.path.getmtime,
        default=None  # Default to None if the folder is empty
    )

    if latest_file_path is None:
        print("No image files found in the specified folder.")
        return

    # Open and encode the reference image to Base64
    with Image.open(latest_file_path) as img:
        encoded_image = encode_pil_to_base64(img)
        img_width, img_height = img.size  # Image dimensions

    # Construct the request payload
    data = {
        "prompt": "<lora:CWG_archisketch_v1:1>,Building,pre skecth,masterpiece,best quality,featuring markers",
        "negative_prompt": "blurry, lower quality, bad anatomy,low quality,worst quality,normal quality ",
        "init_images": [encoded_image],  # Encoded image in a list
        "steps": 30,
        "width": img_width,
        "height": img_height,
        "seed": random.randint(1, 10000000),
        "sampler_name": "Euler",
        "alwayson_scripts": {
            "ControlNet": {
                "args": [
                     {
                         "enabled": "true",
                         "pixel_perfect": "true",
                         "module": "canny",
                         "model": "control_v11p_sd15_canny_fp16 [b18e0966]",
                         "weight": 1,
                         "image": encoded_image
                     },
                    {
                        "enabled": "true",
                        "pixel_perfect": "true",
                        "module": "depth",
                        "model": "control_v11f1p_sd15_depth_fp16 [4b72d323]",
                        "weight": 1,
                        "image": encoded_image
                    }
                ]
            }
        }
    }

    # Send the request and get the response
    response = requests.post(url, json=data)
    response_json = response.json()

    # Define the base path for saving the image (without sequence number)
    base_save_path = r"C:\Users\wujie1\Downloads\Generated_Image.png"
    save_decoded_image(response_json['images'][0], base_save_path)

if __name__ == '__main__':
    main()

 

标签:name,调完,提示,os,image,base,file,path,手稿
From: https://www.cnblogs.com/zly324/p/18103761

相关文章

  • 为什么网页提示SSL证书不信任?
    随着传统行业迅速融入互联网,信息安全问题逐渐凸显,企业商业机密和国家信息安全面临网络威胁。为应对此挑战,国内企业已积极部署SSL证书,强化信息安全防护,确保业务安全稳定运行。在企业部署SSL证书的过程中,不免会遇到网页提示SSL证书不信任的问题。为什么证书安装后浏览器还是显示了S......
  • 前端学习-UI框架学习-Bootstrap5-008-信息提示框
    菜鸟教程链接提示框-基础颜色提示框可以使用.alert类,后面加上.alert-success,.alert-info,.alert-warning,.alert-danger,.alert-primary,.alert-secondary,.alert-light或.alert-dark类来实现:提示框添加链接提示框中在链接的标签上添加alert-link类来设置匹......
  • magento2 登录后台一直提示重定向
    phpbin/magentoconfig:setweb/cookie/cookie_domainyt.w3cwang.comphpbin/magentoconfig:setweb/unsecure/base_urlhttp://yt.w3cwang.com/这个httpphpbin/magentoconfig:setweb/secure/base_urlhttps://yt.w3cwang.com/这个是httpsphpbin/magentoconfi......
  • 大模型提示工程之Prompt框架和示例
    今天和大家分享一下:大模型提示工程之Prompt框架和示例:TAG框架任务(Task): 开发一个新的手机应用,旨在帮助用户更好地管理他们的日常健康。行动(Action): 进行市场调研,设计用户友好的界面,开发核心健康跟踪功能,测试应用并收集用户反馈。目标(Goal): 在六个月内发布应用,并在发布后的......
  • 建筑转手稿
    importrequestsimportbase64importosfromPILimportImageimportioimportrandomdefencode_pil_to_base64(image):"""EncodeaPILimagetoaBase64string."""withio.BytesIO()asoutput_bytes:......
  • DSP-CCS12在线仿真,设置断点提示AET资源不够!
    刚刚开始学习DSP,在用CCS12开发环境在线仿真时出现这样的提示:"ThistaskcannotbeaccomplishedwiththeexistingAETresources."网上查找一番,得出这样的解释:CCS会默认载入工程上一次设置的断点,会占用断点资源;另外,手动暂停和使能CIO功能也会占用一个断点。这样很容易就就出现......
  • Eclips 从 Github 获取源的更新提示超时
    将Git所设端口与系统代理设置成一致的内容#注意修改成自己的IP和端口号gitconfig--globalhttp.proxyhttp://127.0.0.1:51081用完之后取消代理。是因为,访问Gitee或其它是不需要梯子,所以要取消代理;或者后悔设置代理了,也可以利用此取消#取消代理gitconfig--global-......
  • 【异常】IDEA链接TDengine提示SQLException: JNI ERROR (0x2354): Version not compat
    一、异常内容TDEngine提示SQLException:JNIERROR(0x2354):VersionnotcompatibleCausedby:java.sql.SQLException:JNIERROR(0x2354):Versionnotcompatible atcom.taosdata.jdbc.TSDBError.createSQLException(TSDBError.java:90) atcom.taosdata.jdbc.TS......
  • cas登陆页定义异常提示(中英文等
    cas登陆页定义异常提示(中英文等): 两种异常的处理方式不同,1,是通过配置文件指定用哪个异常类和方法2,是通过继承AuthenticationViaFormAction类实现(见后文)不同异常的提示,区分中英文1,定义好自定义异常2,在执行异常的地方抛出3,定义好捕捉异常的处理器   property属性文件:......
  • 《python编程从入门到实践》 Sublime中运行程序提示“Python was not found but can b
    安装过程1.安装python32.安装SublimeText①下载②让SublimeText使用命令python3来运行Python程序3.运行①新建helloworld②运行遇到的问题执行以上步骤后:1.搜索解决方法2.关掉后执行3.更改buildsystem方式python3改为python,执行,成功备注:发现保持应用打开......