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

Allow PNG-RGBA for Extras Tab #15334

Merged
merged 4 commits into from Mar 21, 2024

Conversation

Gourieff
Copy link
Contributor

Description

Hello guys!
There's a small issue when we want to swap a face to PNG images (with RGBA) - (in particular via ReActor)
By default SD WebUI cuts Alpha-channel when we postprocess the image via the Extras Tab

E.g.:
I load the image with transparency and want to swap the face
So I get the following result:

image
image

As we can see the alpha channel is lost

With this little fix we can operate with all 4 channels of the input image - and the result is:

image
image

Screenshots/videos:

image

Additional information

Related Issue: Gourieff/sd-webui-reactor#382

Checklist:

@@ -66,7 +66,7 @@ def get_images(extras_mode, image, image_folder, input_dir):
if parameters:
existing_pnginfo["parameters"] = parameters

initial_pp = scripts_postprocessing.PostprocessedImage(image_data.convert("RGB"))
initial_pp = scripts_postprocessing.PostprocessedImage(image_data.convert("RGBA")) if image_data.mode == "RGBA" else scripts_postprocessing.PostprocessedImage(image_data.convert("RGB"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if image_data.mode != "RGBA":
    image_data = image_data.convert("RGB")

initial_pp = scripts_postprocessing.PostprocessedImage(image_data)

Copy link
Contributor Author

@Gourieff Gourieff Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I also tried this way, but it throws ValueError: Operation on closed image during the test:

*** API error: POST: http://127.0.0.1:7860/sdapi/v1/extra-single-image {'error': 'ValueError', 'detail': '', 'body': '', 'errors': 'Operation on closed image'}
    Traceback (most recent call last):
      File "C:\_NN\A1111\venv\lib\site-packages\anyio\streams\memory.py", line 98, in receive
        return self.receive_nowait()
      File "C:\_NN\A1111\venv\lib\site-packages\anyio\streams\memory.py", line 93, in receive_nowait
        raise WouldBlock
    anyio.WouldBlock

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\base.py", line 78, in call_next
        message = await recv_stream.receive()
      File "C:\_NN\A1111\venv\lib\site-packages\anyio\streams\memory.py", line 118, in receive
        raise EndOfStream
    anyio.EndOfStream

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\_NN\A1111\modules\api\api.py", line 186, in exception_handling
        return await call_next(request)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\base.py", line 84, in call_next
        raise app_exc
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\base.py", line 70, in coro
        await self.app(scope, receive_or_disconnect, send_no_error)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\base.py", line 108, in __call__
        response = await self.dispatch_func(request, call_next)
      File "C:\_NN\A1111\modules\api\api.py", line 150, in log_and_time
        res: Response = await call_next(req)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\base.py", line 84, in call_next
        raise app_exc
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\base.py", line 70, in coro
        await self.app(scope, receive_or_disconnect, send_no_error)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\cors.py", line 84, in __call__
        await self.app(scope, receive, send)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\gzip.py", line 24, in __call__
        await responder(scope, receive, send)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\gzip.py", line 44, in __call__
        await self.app(scope, receive, self.send_with_gzip)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\exceptions.py", line 79, in __call__
        raise exc
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\middleware\exceptions.py", line 68, in __call__
        await self.app(scope, receive, sender)
      File "C:\_NN\A1111\venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in __call__
        raise e
      File "C:\_NN\A1111\venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in __call__
        await self.app(scope, receive, send)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\routing.py", line 718, in __call__
        await route.handle(scope, receive, send)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\routing.py", line 276, in handle
        await self.app(scope, receive, send)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\routing.py", line 66, in app
        response = await func(request)
      File "C:\_NN\A1111\venv\lib\site-packages\fastapi\routing.py", line 237, in app
        raw_response = await run_endpoint_function(
      File "C:\_NN\A1111\venv\lib\site-packages\fastapi\routing.py", line 165, in run_endpoint_function
        return await run_in_threadpool(dependant.call, **values)
      File "C:\_NN\A1111\venv\lib\site-packages\starlette\concurrency.py", line 41, in run_in_threadpool
        return await anyio.to_thread.run_sync(func, *args)
      File "C:\_NN\A1111\venv\lib\site-packages\anyio\to_thread.py", line 33, in run_sync
        return await get_asynclib().run_sync_in_worker_thread(
      File "C:\_NN\A1111\venv\lib\site-packages\anyio\_backends\_asyncio.py", line 877, in run_sync_in_worker_thread
        return await future
      File "C:\_NN\A1111\venv\lib\site-packages\anyio\_backends\_asyncio.py", line 807, in run
        result = context.run(func, *args)
      File "C:\_NN\A1111\modules\api\api.py", line 566, in extras_single_image_api
        return models.ExtrasSingleImageResponse(image=encode_pil_to_base64(result[0][0]), html_info=result[1])
      File "C:\_NN\A1111\modules\api\api.py", line 113, in encode_pil_to_base64
        image.save(output_bytes, format="PNG", pnginfo=(metadata if use_metadata else None), quality=opts.jpeg_quality)
      File "C:\_NN\A1111\venv\lib\site-packages\PIL\Image.py", line 2395, in save
        self._ensure_mutable()
      File "C:\_NN\A1111\venv\lib\site-packages\PIL\Image.py", line 610, in _ensure_mutable
        self._copy()
      File "C:\_NN\A1111\venv\lib\site-packages\PIL\Image.py", line 603, in _copy
        self.load()
      File "C:\_NN\A1111\venv\lib\site-packages\PIL\ImageFile.py", line 162, in load
        pixel = Image.Image.load(self)
      File "C:\_NN\A1111\venv\lib\site-packages\PIL\Image.py", line 872, in load
        return self.im.pixel_access(self.readonly)
      File "C:\_NN\A1111\venv\lib\site-packages\PIL\_util.py", line 19, in __getattr__
        raise self.ex
    ValueError: Operation on closed image

image

@AUTOMATIC1111 AUTOMATIC1111 merged commit b80b1cf into AUTOMATIC1111:dev Mar 21, 2024
3 checks passed
@builder-main
Copy link

builder-main commented Mar 21, 2024

I get a related issue with a grayscale upscaling model "RuntimeError: Given groups=1, weight of size [64, 1, 3, 3], expected input[1, 3, 192, 192] to have 1 channels, but got 3 channels instead". Is this related ? How should it be implemented ?

@Rabcor
Copy link

Rabcor commented May 1, 2024

I discovered that this commit is breaking transparency support for the upscalers, and finding new flaws in the pre-existing support for transparency there as well..

#15664 (comment)

Maybe there's a way to leverage the changes of this commit so the upscalers can benefit from it too instead of it breaking their transparency support? 🤔

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

Successfully merging this pull request may close these issues.

None yet

4 participants