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

Error when sending to img2img

Open 4lt3r3go opened this issue 2 years ago • 35 comments

[SOLVED] this is happening when i press "send to img2img" or "send to inpaint" from the Image Browser tab. ..but same buttons pressed in txt2img tab works fine. :( image image

4lt3r3go avatar Jan 04 '23 16:01 4lt3r3go

Same problem, but I receive bit different error after update webUI:

File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\gradio\routes.py", line 321, in run_predict output = await app.blocks.process_api( File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\gradio\blocks.py", line 1015, in process_api result = await self.call_function(fn_index, inputs, iterator, request) File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\gradio\blocks.py", line 856, in call_function prediction = await anyio.to_thread.run_sync( File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\anyio\to_thread.py", line 31, in run_sync return await get_asynclib().run_sync_in_worker_thread( File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\anyio_backends_asyncio.py", line 937, in run_sync_in_worker_thread return await future File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\anyio_backends_asyncio.py", line 867, in run result = context.run(func, *args) File "C:\ai\stable-diffusion-webui\modules\generation_parameters_copypaste.py", line 113, in send_image_and_dimensions img = image_from_url_text(x) File "C:\ai\stable-diffusion-webui\modules\generation_parameters_copypaste.py", line 59, in image_from_url_text filedata = base64.decodebytes(filedata.encode('utf-8')) File "C:\Users\Aleksey\AppData\Local\Programs\Python\Python310\lib\base64.py", line 562, in decodebytes return binascii.a2b_base64(s) binascii.Error: Incorrect padding

Alexey1327 avatar Jan 04 '23 17:01 Alexey1327

Same problem, but I receive bit different error after update webUI:

File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\gradio\routes.py", line 321, in run_predict output = await app.blocks.process_api( File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\gradio\blocks.py", line 1015, in process_api result = await self.call_function(fn_index, inputs, iterator, request) File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\gradio\blocks.py", line 856, in call_function prediction = await anyio.to_thread.run_sync( File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\anyio\to_thread.py", line 31, in run_sync return await get_asynclib().run_sync_in_worker_thread( File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\anyio_backends_asyncio.py", line 937, in run_sync_in_worker_thread return await future File "C:\ai\stable-diffusion-webui\venv\lib\site-packages\anyio_backends_asyncio.py", line 867, in run result = context.run(func, *args) File "C:\ai\stable-diffusion-webui\modules\generation_parameters_copypaste.py", line 113, in send_image_and_dimensions img = image_from_url_text(x) File "C:\ai\stable-diffusion-webui\modules\generation_parameters_copypaste.py", line 59, in image_from_url_text filedata = base64.decodebytes(filedata.encode('utf-8')) File "C:\Users\Aleksey\AppData\Local\Programs\Python\Python310\lib\base64.py", line 562, in decodebytes return binascii.a2b_base64(s) binascii.Error: Incorrect padding

you get that red error text too in img2img tab? your error looks like the same to mine somehow, i'm not expert btw

4lt3r3go avatar Jan 04 '23 18:01 4lt3r3go

This is error from console, in the tab I see just the same Error message in red color. But, yeah, looks like same problem.

Alexey1327 avatar Jan 05 '23 02:01 Alexey1327

Just an FYI this happened due to some major changes to the webui's gradio. Borked lots of extensions. Options are to roll back webui repo, wait for image-browser author to update, submit a PR with changes that fix.

itsagoodbrain avatar Jan 05 '23 15:01 itsagoodbrain

Yep same here, you can always drag and drop your original image from browser until there is a fix, settings are still sent to img2img

DrMacabre avatar Jan 05 '23 17:01 DrMacabre

so i guess we just have to wait for an update of this extention. Ty guys for reply

4lt3r3go avatar Jan 05 '23 19:01 4lt3r3go

Looks like pull request #51 fixes the issue.

crazyilian avatar Jan 05 '23 21:01 crazyilian

Did not fix here.

edgardmello avatar Jan 06 '23 12:01 edgardmello

same. ERROR is still there

4lt3r3go avatar Jan 06 '23 17:01 4lt3r3go

Looks like pull request #51 fixes the issue.

fixed for me

EriIaz avatar Jan 06 '23 22:01 EriIaz

Looks like pull request #51 fixes the issue.

Seems to have worked for me as well. Don't know if it's relevant, but I overwrote the main files with the ones from #51, so I still have the .DS_Store file present in my install.

Kerikhan avatar Jan 07 '23 01:01 Kerikhan

Making the change in #51 fixed the issue for me as well.

Kevman323 avatar Jan 07 '23 03:01 Kevman323

Did not fix here.

same. ERROR is still there

Pull Request #51 fixed the issue for me, but I had actually solved it myself prior to seeing this change. If #51 does not work for you, I would suggest trying my fix, which actually changes stable-diffusion-webui\modules\generation_parameters_copypaste.py. The function I edited begins at line 39 of generation_parameters_copypaste.py. I barely understand GitHub so I don't know how to link to various branches or suggest my changes.

def image_from_url_text(filedata):
    if filedata == None: return None #CUSTOM CODE
    if type(filedata) == list and len(filedata) > 0 and type(filedata[0]) == dict and filedata[0].get("is_file", False):
        filedata = filedata[0]

    if type(filedata) == dict and filedata.get("is_file", False):
        filename = filedata["name"]
        is_in_right_dir = ui_tempdir.check_tmp_file(shared.demo, filename)
        assert is_in_right_dir, 'trying to open image file outside of allowed directories'

        return Image.open(filename)

    if type(filedata) == list:
        if len(filedata) == 0:
            return None

        filedata = filedata[0]

    # if filedata.startswith("data:image/png;base64,"): #ORIGINAL CODE
    #     filedata = filedata[len("data:image/png;base64,"):]

    # filedata = base64.decodebytes(filedata.encode('utf-8'))
    # image = Image.open(io.BytesIO(filedata))
    # return image

    if filedata.startswith("data:image/png;base64,"): #CUSTOM CODE
        filedata = filedata[len("data:image/png;base64,"):]
        # filedata = filedata+"=="
        filedata = base64.decodebytes(filedata.encode('utf-8'))
    else:
        filedata = filedata.encode('utf-8')
    image = Image.open(filedata)
    return image

Just replace the current function with this version and it should work. The original code is trying to decode base64-encoded text, but it tries this whether or not the filedata has the "data:image/png;base64," string in front, which I expect to be an identifier that the decoding is necessary. My fix performs the decoding if the base64 identifier is present, or skips it if unnecessary. While I was digging around, I also realized that clicking any "send to ____" button while no image was present in the above viewer would cause an error, since the if filedata.startswith("data:image/png;base64,") check would be performed on a None type, so I added the one line if filedata == None: return None #CUSTOM CODE to circumvent the error.

Googolplexed0 avatar Jan 08 '23 09:01 Googolplexed0

@Googolplexed06 It does work well with this fix, you are the real hero!

ligerye avatar Jan 08 '23 14:01 ligerye

Thank you Googolplexed06

jipsita avatar Jan 09 '23 19:01 jipsita

The function I edited begins at line 39 of generation_parameters_copypaste.py

SOLVED!

i tryed remove that file and let it renew with git pull. nothing. then i made that modification at line 39 manually and voilà.. solved. thank you everyone!

4lt3r3go avatar Jan 10 '23 06:01 4lt3r3go

seems to be broken again despite having edited the file. the fix worked until today.

DrMacabre avatar Jan 12 '23 14:01 DrMacabre

[SOLVED] this is happening when i press "send to img2img" or "send to inpaint" from the Image Browser tab. ..but same buttons pressed in txt2img tab works fine. :( image image

Same shit, but why u made the UI look ugly?

ice051128 avatar Jan 13 '23 07:01 ice051128

Did not fix here.

same. ERROR is still there

Pull Request #51 fixed the issue for me, but I had actually solved it myself prior to seeing this change. If #51 does not work for you, I would suggest trying my fix, which actually changes stable-diffusion-webui\modules\generation_parameters_copypaste.py. The function I edited begins at line 39 of generation_parameters_copypaste.py. I barely understand GitHub so I don't know how to link to various branches or suggest my changes.

def image_from_url_text(filedata):
    if filedata == None: return None #CUSTOM CODE
    if type(filedata) == list and len(filedata) > 0 and type(filedata[0]) == dict and filedata[0].get("is_file", False):
        filedata = filedata[0]

    if type(filedata) == dict and filedata.get("is_file", False):
        filename = filedata["name"]
        is_in_right_dir = ui_tempdir.check_tmp_file(shared.demo, filename)
        assert is_in_right_dir, 'trying to open image file outside of allowed directories'

        return Image.open(filename)

    if type(filedata) == list:
        if len(filedata) == 0:
            return None

        filedata = filedata[0]

    # if filedata.startswith("data:image/png;base64,"): #ORIGINAL CODE
    #     filedata = filedata[len("data:image/png;base64,"):]

    # filedata = base64.decodebytes(filedata.encode('utf-8'))
    # image = Image.open(io.BytesIO(filedata))
    # return image

    if filedata.startswith("data:image/png;base64,"): #CUSTOM CODE
        filedata = filedata[len("data:image/png;base64,"):]
        # filedata = filedata+"=="
        filedata = base64.decodebytes(filedata.encode('utf-8'))
    else:
        filedata = filedata.encode('utf-8')
    image = Image.open(filedata)
    return image

Just replace the current function with this version and it should work. The original code is trying to decode base64-encoded text, but it tries this whether or not the filedata has the "data:image/png;base64," string in front, which I expect to be an identifier that the decoding is necessary. My fix performs the decoding if the base64 identifier is present, or skips it if unnecessary. While I was digging around, I also realized that clicking any "send to ____" button while no image was present in the above viewer would cause an error, since the if filedata.startswith("data:image/png;base64,") check would be performed on a None type, so I added the one line if filedata == None: return None #CUSTOM CODE to circumvent the error.

A simple parameter change from #51 works flawlessly. What's the point of changing the whole function?

ice051128 avatar Jan 13 '23 07:01 ice051128

A simple parameter change from #39 works flawlessly. What's the point of changing the whole function?

doens't work here, i just checked the line in #39 and it's already changed on my script

DrMacabre avatar Jan 13 '23 08:01 DrMacabre

This whole thing is somewhat confusing. Who has tried out fix #51 and it still doesn't work? Can you please post your whole console log?

AlUlkesh avatar Jan 13 '23 15:01 AlUlkesh

This whole thing is somewhat confusing. Who has tried out fix #51 and it still doesn't work? Can you please post your whole console log?

@4lt3r3go posted screenshot in the issue description. #51 didn't work for him.

crazyilian avatar Jan 13 '23 16:01 crazyilian

#39 + #51 fixed it for me

DrMacabre avatar Jan 13 '23 16:01 DrMacabre

A simple parameter change from #39 works flawlessly. What's the point of changing the whole function?

doens't work here, i just checked the line in #39 and it's already changed on my script

Mentioned the wrong PR, I meant #51

ice051128 avatar Jan 14 '23 02:01 ice051128

Well, I just had something interesting happen. Even with fix #51 I suddenly again ran into

    if filedata.startswith("data:image/png;base64,"):
AttributeError: 'NoneType' object has no attribute 'startswith'

Some looking into it later, I can see this in Chrome's DevTools log: index.0b923826.js:76 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'forEach') at lt (index.0b923826.js:76:2728) lt @ index.0b923826.js:76 Promise.then (async) (anonymous) @ index.0b923826.js:76 (anonymous) @ index.0b923826.js:4 ae @ index.0b923826.js:4 f @ Number.svelte:16 (anonymous) @ index.0b923826.js:4 (anonymous) @ index.0b923826.js:4 b @ Number.svelte:19 Z.a.$$.update @ Number.svelte:32 Rl @ index.0b923826.js:4 mt @ index.0b923826.js:4 Promise.then (async) so @ index.0b923826.js:4 zl @ index.0b923826.js:4 (anonymous) @ index.0b923826.js:4 (anonymous) @ index.0b923826.js:76 lt @ index.0b923826.js:76 Promise.then (async) (anonymous) @ index.0b923826.js:76 (anonymous) @ index.0b923826.js:4 ae @ index.0b923826.js:4 _ @ Button.svelte:10 (anonymous) @ index.0b923826.js:4 ae @ index.0b923826.js:4 B @ Button.svelte:9 (anonymous) @ (index):1947 (index):1799 in images_history_click_image ! (index):1800 DOMTokenList(3) ['gallery-item', 'group', 'svelte-1g9btlg', value: 'gallery-item group svelte-1g9btlg'] (index):1875 tabName img2img (index):1876 img_index -1 (index):1877 img2img_images_history_set_index (index):1799 in images_history_click_image ! (index):1800 DOMTokenList(3) ['gallery-item', 'group', 'svelte-1g9btlg', value: 'gallery-item group svelte-1g9btlg'] (index):1875 tabName txt2img (index):1876 img_index -1 (index):1877 txt2img_images_history_set_index (index):1799 in images_history_click_image ! (index):1800 DOMTokenList(9) ['gallery-item', '!flex-none', '!h-9', '!w-9', 'transition-all', 'duration-75', 'scale-90', 'transform', 'svelte-1g9btlg', value: 'gallery-item !flex-none !h-9 !w-9 transition-all duration-75 scale-90 transform svelte-1g9btlg'] (index):1875 tabName txt2img (index):1876 img_index 2 (index):1877 txt2img_images_history_set_index (index):1799 in images_history_click_image !

So, the first time I clicked on an image I got img_index -1, which would lead to the error message about base64. Second click on the same image, same thing. Third click and now I get the correct img_index 2

So what is going on here? Directly before this is an error message connected to asynchronous operations. I don't know if that has anything to do with the problem, but if it does that might hint at a problem with the threading involved

AlUlkesh avatar Jan 14 '23 11:01 AlUlkesh

Well, I just had something interesting happen. Even with fix #51 I suddenly again ran into

    if filedata.startswith("data:image/png;base64,"):
AttributeError: 'NoneType' object has no attribute 'startswith'

This problem was mentioned and fixed in pull request AUTOMATIC1111/stable-diffusion-webui#6541. Because of the other problems, it is still not merged.

crazyilian avatar Jan 14 '23 12:01 crazyilian

This problem was mentioned and fixed in pull request AUTOMATIC1111/stable-diffusion-webui#6541.

Where do you see this there? I can only find a mention for if no image was present in the viewer. In my case the picture was visible.

AlUlkesh avatar Jan 14 '23 14:01 AlUlkesh

So my current understanding is:

  • #51 solves the issue for some people, some of the time
  • @Googolplexed06 's solution works for most people, but Automatic makes a good point, that a function that decodes base64 should not handle something else

I hope I might now have found a new solution, that does not require a change in webui's code.

This is how we access generation_parameters_copypaste: modules.generation_parameters_copypaste.bind_buttons(send_to_buttons, img_file_name, img_file_info)

The second parameter is currently img_file_name, which is actually a gradio textbox object with should return the filename.

#51 changes this to history_gallery, the gradio gallery object, which should return a list of images.

generation_parameters_copypaste.py's "entry" code for us is:

def send_image_and_dimensions(x):
    if isinstance(x, Image.Image):
        img = x
    else:
        img = image_from_url_text(x)

So both versions, filename and list of images, will be handled by the else, which is not really that function's purpose.

I now found that the images-browser has yet another object called hidden, which is a (hidden) gradio.image and contains the selected image and so would not have to go into the else.

So I changed the line into: modules.generation_parameters_copypaste.bind_buttons(send_to_buttons, hidden, img_file_info)

and so far it seems to work all the time.

Could someone else please confirm this?

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

AlUlkesh avatar Jan 14 '23 18:01 AlUlkesh

I found this thread/issue after looking into this error. I haven't dug into the history of previous fixes nor the details of what the change above is doing, but it seems to work!

ibushong avatar Jan 15 '23 00:01 ibushong

Could someone else please confirm this?

AlUlkesh@d223344

#51 worked for me, this fix works too. Will you create a pull request? @AlUlkesh

crazyilian avatar Jan 15 '23 10:01 crazyilian