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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


scripts.scripts_postproc.run(initial_pp, args)

Expand Down
2 changes: 1 addition & 1 deletion modules/ui_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create_ui():
with gr.Column(variant='compact'):
with gr.Tabs(elem_id="mode_extras"):
with gr.TabItem('Single Image', id="single_image", elem_id="extras_single_tab") as tab_single:
extras_image = gr.Image(label="Source", source="upload", interactive=True, type="pil", elem_id="extras_image")
extras_image = gr.Image(label="Source", source="upload", interactive=True, type="pil", elem_id="extras_image", image_mode="RGBA")

with gr.TabItem('Batch Process', id="batch_process", elem_id="extras_batch_process_tab") as tab_batch:
image_batch = gr.Files(label="Batch Process", interactive=True, elem_id="extras_image_batch")
Expand Down
2 changes: 1 addition & 1 deletion scripts/postprocessing_codeformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def process(self, pp: scripts_postprocessing.PostprocessedImage, enable, codefor
if codeformer_visibility == 0 or not enable:
return

restored_img = codeformer_model.codeformer.restore(np.array(pp.image, dtype=np.uint8), w=codeformer_weight)
restored_img = codeformer_model.codeformer.restore(np.array(pp.image.convert("RGB"), dtype=np.uint8), w=codeformer_weight)
res = Image.fromarray(restored_img)

if codeformer_visibility < 1.0:
Expand Down
2 changes: 1 addition & 1 deletion scripts/postprocessing_gfpgan.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def process(self, pp: scripts_postprocessing.PostprocessedImage, enable, gfpgan_
if gfpgan_visibility == 0 or not enable:
return

restored_img = gfpgan_model.gfpgan_fix_faces(np.array(pp.image, dtype=np.uint8))
restored_img = gfpgan_model.gfpgan_fix_faces(np.array(pp.image.convert("RGB"), dtype=np.uint8))
res = Image.fromarray(restored_img)

if gfpgan_visibility < 1.0:
Expand Down