首页 > 其他分享 >建筑应用在家中运行

建筑应用在家中运行

时间:2024-03-30 18:45:33浏览次数:13  
标签:应用 image 家中 base file output path 建筑 name

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

    # Request the user to input the image file path
    user_input_path = input("Please enter the path of the image file: ")

    if not os.path.exists(user_input_path) or not os.path.isfile(user_input_path):
        print("The specified path does not exist or is not a file.")
        return

    # Open and encode the reference image to Base64
    with Image.open(user_input_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 sketch,masterpiece,best quality,featuring markers,(3D:0.7)",
        "negative_prompt": "blurry, lower quality, glossy finish,insufficient contrast",
        "init_images": [encoded_image],  # Encoded image in a list
        "steps": 30,
        "width": img_width,
        "height": img_height,
        "seed": random.randint(1, 10000000),
        "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\Admin\Desktop\图片生成\Building,pre sketch,masterpiece,best quality,featuring markers.png"
    save_decoded_image(response_json['images'][0], base_save_path)

if __name__ == '__main__':
    main()

 

标签:应用,image,家中,base,file,output,path,建筑,name
From: https://www.cnblogs.com/zly324/p/18105857

相关文章

  • 多态在模板类中的应用
    先看一个多态的例子:classHuman{public:virtualvoideat=0;virtual~Human(){}};classMen:publicHuman{public:virtualvoideat(){cout<<"男人"<<endl;}};classWomen:publicHuman{public:virtu......
  • react + electron 应用在线更新功能记录
    这个东西很多人都可能用electron-update这个玩意,但是我用了,发现总是更新不了,于是放弃,使用electron自带功能  主进程main.js中引入 const{autoUpdater}=require('electron-updater'); 以下为主要代码,也是放在main.js  autoUpdater.autoDownload=false......
  • 适合汽车应用的MAX96705AGTJ/V、DS90UB913ATRTVJQ1小尺寸串行器,以及TEF8105EN/N1汽车
    1、MAX96705AGTJ/V为小尺寸串行器,具有特别适合于汽车摄像应用的特性。其功能和引脚与MAX9271兼容。高带宽模式下,对于12位线性或组合HDR数据类型,并行时钟最大为116MHz。应用汽车摄像应用规格功能:串行器数据速率:1.6Gbps输入类型:CML输出类型:CMOS,LVCMOS输入数:1输出数:16电压-供电:1......
  • Acunetix v24.3 (Linux, Windows) - Web 应用程序安全测试
    Acunetixv24.3(Linux,Windows)-Web应用程序安全测试Acunetix|WebApplicationSecurityScanner请访问原文链接:https://sysin.org/blog/acunetix/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org重要提示AcunetixPremium现在使用日历化版本命名。请注意,从......
  • COMP3331/9331计算机网络与应用
    COMP3331/9331计算机网络与应用2024年第1学期的转让版本1.0到期时间:2024年4月18日星期四上午11:59(中午)(第10周)1.变更日志版本1.0于2024年3月7日发布。2.目标和学习目标对于此任务,您要在UDP协议上实现一个可靠的传输协议。我们将引用您将在本任务中实现的可靠传输协议简单传......
  • 洛谷题单指南-图的基本应用-P2853 [USACO06DEC] Cow Picnic S
    原题链接:https://www.luogu.com.cn/problem/P2853题意解读:找到所有奶牛都可以到达的牧场,就是要从奶牛所在位置开始遍历,求所有奶牛能重合的点的个数。解题思路:直接从从牛奶所在位置进行DFS,记录每个位置有奶牛能到的个数,个数等于奶牛总数的即合适的牧场。100分代码:#include<bi......
  • 扫描线的应用1
    概述顾名思义,扫描线通常是在二维空间内,模拟一条线上下扫描,以此达到解决求二维空间的矩形面积或点数问题。实现方法通常采用排序,离散化等操作。排序是为了扫描的不重不漏,而离散化是由于我们模拟的扫描线会一段段扫描,可以去掉重复的信息。 矩形面积1151--Atlantis给定平......
  • 洛谷题单指南-图的基本应用-P1127 词链
    原题链接:https://www.luogu.com.cn/problem/P1127题意解读:按字典序排列单词,使得相邻单词的首位字母一样。解题思路:由于单词之间可以相邻的条件是前一个单词的末尾字母和后一个单词的开头字母一样,因此可以遍历每一个单词,再找到每一个可以接在其后面的单词,建立一个邻接表,然后从......
  • Spring Boot框架中的JDK动态代理实践及其应用场景
    引言在Java编程中,JDK动态代理是一种强大的设计模式,它允许我们在运行时动态地创建并实现代理类,从而对目标对象的行为进行增强或控制。这种机制主要由Java标准库java.lang.reflect.Proxy类和java.lang.reflect.InvocationHandler接口提供支持。在诸如SpringBoot这样的企业级开......
  • HOW - 图形格式SVG及其应用
    目录一、介绍:scalablevectorgraphic二、基本使用三、SVG字体:自定义图标或者符号四、SVGSprite:自定义图标五、动态FetchCDNSVG图标六、SVG压缩七、SvgvsCanvas一、介绍:scalablevectorgraphicSVG是可缩放矢量图形(ScalableVectorGraphics)的缩写......