Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when sending to img2img #69

Open
4lt3r3go opened this issue Jan 4, 2023 · 35 comments
Open

Error when sending to img2img #69

4lt3r3go opened this issue Jan 4, 2023 · 35 comments

Comments

@4lt3r3go
Copy link

4lt3r3go commented Jan 4, 2023

[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

@Alexey1327
Copy link

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

@4lt3r3go
Copy link
Author

4lt3r3go commented Jan 4, 2023

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

@Alexey1327
Copy link

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

@itsagoodbrain
Copy link

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.

@DrMacabre
Copy link

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

@4lt3r3go
Copy link
Author

4lt3r3go commented Jan 5, 2023

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

@crazyilian
Copy link

Looks like pull request #51 fixes the issue.

@edgardmello
Copy link

Did not fix here.

@4lt3r3go
Copy link
Author

4lt3r3go commented Jan 6, 2023

same.
ERROR is still there

@EriIaz
Copy link

EriIaz commented Jan 6, 2023

Looks like pull request #51 fixes the issue.

fixed for me

@Kerikhan
Copy link

Kerikhan commented Jan 7, 2023

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.

@Kevman323
Copy link

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

@Googolplexed0
Copy link

Googolplexed0 commented Jan 8, 2023

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.

@ligerye
Copy link

ligerye commented Jan 8, 2023

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

@jipsita
Copy link

jipsita commented Jan 9, 2023

Thank you Googolplexed06

@4lt3r3go
Copy link
Author

4lt3r3go commented Jan 10, 2023

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!

@DrMacabre
Copy link

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

@ice051128
Copy link

[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
Copy link

ice051128 commented Jan 13, 2023

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?

@DrMacabre
Copy link

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

@AlUlkesh
Copy link

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?

@crazyilian
Copy link

crazyilian commented Jan 13, 2023

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.

@DrMacabre
Copy link

#39 + #51 fixed it for me

@ice051128
Copy link

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

@AlUlkesh
Copy link

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

@crazyilian
Copy link

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.

@AlUlkesh
Copy link

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 referenced this issue in AlUlkesh/stable-diffusion-webui-images-browser Jan 14, 2023
@AlUlkesh
Copy link

So my current understanding is:

  • Fix send to buttons not sending the image #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?

AlUlkesh@d223344

@ibushong
Copy link

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!

@crazyilian
Copy link

Could someone else please confirm this?

AlUlkesh@d223344

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

@AlUlkesh
Copy link

@crazyilian

Will you create a pull request?

Ok, will do.

@PandaBoyo
Copy link

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.

You're a lifesaver, I've been scratching my head for a week trying to find a fix for this and your edit worked perfectly!

@Harvester62
Copy link

I confirm that the PR#76 fixes the problem. Tested all options available for an image in the Image Browser, including Send to openOutpaint.

@AlUlkesh AlUlkesh mentioned this issue Feb 2, 2023
@rinaldop
Copy link

rinaldop commented Feb 5, 2023

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?

This fix for me too. Thank you!!!!

@seppviljar
Copy link

Same. Fresh (updated today via Automatic's Extensions tab) version needed #51 applied on top of it to start working. (MBP M1, Firefox, python: 3.10.9  •  torch: 1.12.1  •  xformers: N/A  •  gradio: 3.16.2  •  commit: ea9bd9fc  •  checkpoint: eb2bb12033 - not sure what else is helpful to know about my env.)

tjm35 pushed a commit to tjm35/stable-diffusion-webui-images-browser that referenced this issue Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests