stable-diffusion-webui icon indicating copy to clipboard operation
stable-diffusion-webui copied to clipboard

[Bug]: Invalid padding for returned base64 image from API

Open aliencaocao opened this issue 2 years ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues and checked the recent builds/commits

What happened?

This only happened after I updated my repo to

I can confirm that on 737eb28faca8be2bb996ee0930ec77d1f7ebd939 it is all good.

Steps to reproduce the problem

My code uses the API for txt2img like this:

for i in response['images']:
    return Image.open(io.BytesIO(base64.b64decode(i)))

What should have happened?

No error

Commit where the problem happens

911a3455edec1be6938bfd6f03fb67d739d8696d

What platforms do you use to access UI ?

Windows

What browsers do you use to access the UI ?

Google Chrome

Command Line Arguments

--force-enable-xformers --listen --deepdanbooru --api --nowebui --disable-safe-unpickle --opt-channelslast

Additional information, context and logs

File "C:\Users\alien\Documents\PyCharm-Projects\koala-music\stable_diffusion.py", line 158, in imagine
    image = generate(**args)
  File "C:\Users\alien\Documents\PyCharm-Projects\koala-music\stable_diffusion.py", line 87, in generate
    return Image.open(io.BytesIO(base64.b64decode(i))), response['info']['infotexts']  # remove when adding support for multiple images
  File "C:\Program Files\Python39\lib\base64.py", line 87, in b64decode
    return binascii.a2b_base64(s)
binascii.Error: Invalid base64-encoded string: number of data characters (1098649) cannot be 1 more than a multiple of 4

Image generation from the webui works fine.

aliencaocao avatar Oct 30 '22 10:10 aliencaocao

Problem is caused by extra line prepended to the return data: data:image/png;base64,

aliencaocao avatar Oct 30 '22 10:10 aliencaocao

I opened PR for a fix https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/3974

aliencaocao avatar Oct 30 '22 10:10 aliencaocao

While the fix is still not approved, you can add the following line before opening the image. This removes the extra line @aliencaocao mentioned.

i = i.replace("data:image/png;base64,","") image = Image.open(io.BytesIO(base64.b64decode(i)))

Karbatos avatar Nov 01 '22 15:11 Karbatos

Update: this is fixed separately by @AUTOMATIC1111 himself in https://github.com/AUTOMATIC1111/stable-diffusion-webui/commit/198a1ffcfc963a3d74674fad560e87dbebf7949f

aliencaocao avatar Nov 02 '22 12:11 aliencaocao

https://github.com/yfszzx/stable-diffusion-webui-images-browser also got this bug

 Invalid base64-encoded string: number of data characters (49) cannot be 1 more than a multiple of 4

Use latest fork instead:

https://github.com/AlUlkesh/stable-diffusion-webui-images-browser

linonetwo avatar Mar 18 '23 16:03 linonetwo