当前位置:AIGC资讯 > AIGC > 正文

aigc 启动器 sd-webui-aki-v4 decode_base64_to_file

下载地址:

SD-WebUI启动器 绘世-启动器 | 万物档案

decode_base64_to_file报错:

  File "E:\BaiduNetdiskDownload\stable diffusion\sd-webui-aki-v4\extensions\sd-webui-controlnet\scripts\external_code.py", line 7, in <module>
    from modules.api import api
  File "E:\BaiduNetdiskDownload\stable diffusion\sd-webui-aki-v4\modules\api\api.py", line 9, in <module>
    from gradio.processing_utils import decode_base64_to_file
ImportError: cannot import name 'decode_base64_to_file' from 'gradio.processing_utils'

报错2:

import gradio as gr
original_IOComponent_init = gr.components.IOComponent.__init__

4.*版本报错, gradio-3.41.2-py3-none-any.whl 不报错

报错2:

  File "E:\BaiduNetdiskDownload\stable diffusion\sd-webui-aki-v4\modules\api\models.py", line 96, in generate_model
    DynamicModel.__config__.allow_population_by_field_name = True
  File "D:\ProgramData\miniconda3\envs\pysd\lib\site-packages\pydantic\_internal\_model_construction.py", line 215, in __getattr__
    raise AttributeError(item)
AttributeError: __config__

报错的gradio版本:

pip install gradio-3.41.2-py3-none-any.whl

sd-webui-aki-v4

from gradio.processing_utils import decode_base64_to_image

def decode_base64_to_image(encoding: str) -> Image.Image:
    image_encoded = extract_base64_data(encoding)
    img = Image.open(BytesIO(base64.b64decode(image_encoded)))
    try:
        if hasattr(ImageOps, "exif_transpose"):
            img = ImageOps.exif_transpose(img)
    except Exception:
        log.warning(
            "Failed to transpose image %s based on EXIF data.",
            img,
            exc_info=True,
        )
    return img

python 实现代码:

def decode_base64_to_image(encoding):
    if encoding.startswith("data:image/"):
        encoding = encoding.split(";")[1].split(",")[1]
    try:
        image = Image.open(BytesIO(base64.b64decode(encoding)))
        return image
    except Exception as err:
        raise HTTPException(status_code=500, detail="Invalid encoded image")

def encode_pil_to_base64(image):
    with io.BytesIO() as output_bytes:

        if opts.samples_format.lower() == 'png':
            use_metadata = False
            metadata = PngImagePlugin.PngInfo()
            for key, value in image.info.items():
                if isinstance(key, str) and isinstance(value, str):
                    metadata.add_text(key, value)
                    use_metadata = True
            image.save(output_bytes, format="PNG", pnginfo=(metadata if use_metadata else None), quality=opts.jpeg_quality)

        elif opts.samples_format.lower() in ("jpg", "jpeg", "webp"):
            parameters = image.info.get('parameters', None)
            exif_bytes = piexif.dump({
                "Exif": { piexif.ExifIFD.UserComment: piexif.helper.UserComment.dump(parameters or "", encoding="unicode") }
            })
            if opts.samples_format.lower() in ("jpg", "jpeg"):
                image.save(output_bytes, format="JPEG", exif = exif_bytes, quality=opts.jpeg_quality)
            else:
                image.save(output_bytes, format="WEBP", exif = exif_bytes, quality=opts.jpeg_quality)

        else:
            raise HTTPException(status_code=500, detail="Invalid image format")

        bytes_data = output_bytes.getvalue()

    return base64.b64encode(bytes_data)

更新时间 2024-05-31