首页 > 其他分享 >建筑转手稿

建筑转手稿

时间:2024-03-28 21:24:07浏览次数:12  
标签:name 手稿 image base file path os 建筑

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,masterpiece,best quality,pre sktch,",
        "negative_prompt": "blurry, lower quality, 3D",
        "init_images": [encoded_image],  # Encoded image in a list
        "steps": 25,
        "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\wujie1\Downloads\Generated_Image.png"
    save_decoded_image(response_json['images'][0], base_save_path)

if __name__ == '__main__':
    main()

 

标签:name,手稿,image,base,file,path,os,建筑
From: https://www.cnblogs.com/zly324/p/18102644

相关文章

  • sd建筑工作流步骤1:文生图
    importrequestsimportbase64importosfromPILimportImageimportioimportjsonimportrandom#用于将图片编码为Base64格式的函数defencode_pil_to_base64(image):withio.BytesIO()asoutput_bytes:image.save(output_bytes,format="PNG")#注......
  • 【Revit二次开发】创建建筑柱并旋转
    创建建筑柱参考:https://www.cnblogs.com/redcode/p/18100979建筑柱创建后再进行旋转,参考官方文档:https://help.autodesk.com/view/RVT/2014/ENU/?guid=GUID-B1C87D72-CAA5-4311-929C-CFC9B5480D24示例如下:ElementTransformUtils.RotateElement(doc,instance.Id,Rvt.Get......
  • 【Revit二次开发】创建建筑柱
    出处https://forums.autodesk.com/t5/revit-api-forum/how-to-create-a-column/m-p/8586697问题//GetaColumntypefromRevitFilteredElementCollectorcollector=newFilteredElementCollector(doc);collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCateg......
  • java智慧工地源码 大型建筑公司应用的智慧工地系统源码 智慧工地建筑管理系统源码
    java智慧工地源码大型建筑公司应用的智慧工地系统源码智慧工地建筑管理系统源码智慧工地是智慧地球理念在工程领域的具体体现,它代表了一种全新的工程全生命周期管理理念。通过运用信息化手段,智慧工地能够精确设计和模拟工程项目,实现互联协同、智能生产、科学管理的施工项目......
  • 开源AI引擎:利用影像处理与目标检测技术对违章建筑排查
    一、项目案例介绍随着城市化进程的加快,城市规划和管理工作面临着前所未有的挑战,违章建筑的排查与处理成为了城市管理中的一项重要任务。传统的违章建筑排查方法依赖于人力巡查,效率低下且难以全面覆盖。为了解决这一问题,现代技术被引入到城市管理中,特别是影像处理、图像分类和......
  • 绍兴中墙建材建筑使用轻质砖如何?可送下城西湖江干拱墅滨江萧山余杭临安富阳建德县级桐
    绍兴中墙建材建筑使用轻质砖如何?可送下城西湖江干拱墅滨江萧山余杭临安富阳建德县级桐庐淳安轻质砖作为建筑材料,因其独特的性能优势,在现代建筑领域得到了广泛应用。这种砖块具有重量轻、保温隔热效果好、抗震能力强等特点,非常适合用于高层建筑和节能建筑设计。首先,轻质砖......
  • lc2940 找到Alice和Bob可以相遇的建筑
    给出数组H[n]和多组询问Q[m],其中Q[i]={a[i],b[i]}表示查询最靠左的下标j,使得a[i]和b[i]都可以移到j处。从x处能移到y处的前提是x<y并且H[x]<H[y]。1<=n<=5e4;1<=H[i]<=1e9;1<=m<=5e4;0<=a[i],b[i]<=n-1相当于找最靠左的上限,可以用st表或线段树来维护区间最大值,然后二分找最左......
  • pexpect手稿
    匹配模式:这在流中寻找,直到匹配到模式。该模式已过载,可能有几种类型。该模式可以是StringType、EOF、已编译的re或任何这些类型的列表。字符串将被编译为重新类型。这会将索引返回到模式列表中。如果模式不是列表,则在成功匹配时返回索引0。这可能会引发EOF或TIMEOUT的异常。为了避......
  • 沃伦·巴菲特2023股东书(2024.2.24)-0-查理·芒格——伯克希尔·哈撒韦公司的建筑师
    2023letterhttps://www.berkshirehathaway.com/letters/2023ltr.pdf查理·芒格——伯克希尔·哈撒韦公司的建筑师查理·芒格于11月28日去世,距离他100岁生日仅33天。虽然在奥马哈出生和长大,但他一生中80%的时间都在其他地方定居。因此,直到1959年他35岁时,我才第一次......
  • [智慧建筑]NTP网络时间服务器(卫星时钟系统)助力建筑数字化
    [智慧建筑]NTP网络时间服务器(卫星时钟系统)助力建筑数字化[智慧建筑]NTP网络时间服务器(卫星时钟系统)助力建筑数字化京准电子科技官微——ahjzsz随着大数据、云计算时代的到来,各行业信息化建设的不断提升,信息化下的各个系统不再单独处理各自业务,而是趋于协同工作,因此,各个单......