javsezlol1 / Stylez

111 stars 8 forks source link

Bug: Unable to "Grab Last Generated Image" #7

Closed ManOrMonster closed 11 months ago

ManOrMonster commented 11 months ago

I'm trying to use the "Grab Last Generated Image" button, but it causes an error:

Traceback (most recent call last):
  File "C:\stable-diffusion\automatic1111\venv\lib\site-packages\gradio\routes.py", line 488, in run_predict
    output = await app.get_blocks().process_api(
  File "C:\stable-diffusion\automatic1111\venv\lib\site-packages\gradio\blocks.py", line 1434, in process_api
    data = self.postprocess_data(fn_index, result["prediction"], state)
  File "C:\stable-diffusion\automatic1111\venv\lib\site-packages\gradio\blocks.py", line 1326, in postprocess_data
    prediction_value = postprocess_update_dict(
  File "C:\stable-diffusion\automatic1111\venv\lib\site-packages\gradio\blocks.py", line 460, in postprocess_update_dict
    prediction_value["value"] = block.postprocess(prediction_value["value"])
  File "C:\stable-diffusion\automatic1111\venv\lib\site-packages\gradio\components\image.py", line 318, in postprocess
    return client_utils.encode_url_or_file_to_base64(y)
  File "C:\stable-diffusion\automatic1111\venv\lib\site-packages\gradio_client\utils.py", line 387, in encode_url_or_file_to_base64
    return encode_file_to_base64(path)
  File "C:\stable-diffusion\automatic1111\venv\lib\site-packages\gradio_client\utils.py", line 360, in encode_file_to_base64
    with open(f, "rb") as file:
OSError: [Errno 22] Invalid argument: 'C:/stable-diffusion/automatic1111/outputs/txt2img-images/2023-10-04/2023-10-04_185051_568229 [1435861344] [1024x1280] [unrealRealism_v40].jpg?1696467051.5862336'

I can see in your code that you're grabbing the file path from the preview image. I don't know what's appending the ?1696467051.5862336 to the end of the filename in the URL, but this did the trick for me:

Replaced in Stylez.js: imageSrc = imageSrc.replace(/.*file=/, ''); with this: imageSrc = imageSrc.replace(/.*file=(.*?)\?.*/, '$1');

Also, as a side note, "Grab Last Generated Image" should probably be renamed to something like "Use Current Preview Image" since it relies on there actually being a preview image displayed.

javsezlol1 commented 11 months ago

could you show me the actaul name of the jpg file? it looks like for some reason you have a cache tage on the end of the image

ManOrMonster commented 11 months ago

The actual name in this particular example is 2023-10-04_185051_568229 [1435861344] [1024x1280] [unrealRealism_v40].jpg

javsezlol1 commented 11 months ago

do you have a plugin or something that is saving all that in the title? if so could you send it me my images dont save with all that data in the filename so its likely i need to allow special chars

ManOrMonster commented 11 months ago

I don't know what's causing the extra data after the extension, but my file names are that way because my "Images filename pattern" is set to [datetime<%Y-%m-%d_%H%M%S_%f>] [[seed]] [[width]x[height]] [[model_name]] in "Saving images/grids" in auto1111 Settings.

image
javsezlol1 commented 11 months ago

okay thanks will work on this now, btw thanks for all the feedback really appreciate it !

javsezlol1 commented 11 months ago

used all your settings not sure where that issue has come from, its working for me ?

ManOrMonster commented 11 months ago

I had to replace this line again after updating. Since there is obviously at least something (a setting or extension) that adds a parameter after the extension in the preview URL, I recommend stripping params with this regex: imageSrc = imageSrc.replace(/.*file=(.*?)(?:\?.*|$)/, '$1');. Especially considering that question marks are not valid in filenames (and would be encoded in a URL anyway), this shouldn't be catching anything but added URL params.

Note that I've updated the regex above. This is different than the previous one and now takes into account URLs without added params 😑