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

Do not convert to JPEG if not possible

Open semjonsona opened this issue 2 years ago • 10 comments

Do not export for 4chan if conversion to JPEG is not possible. This prevents an exception when trying to save large depthmaps when export_for_4chan setting is on. See #6065

semjonsona avatar Feb 07 '23 14:02 semjonsona

May I ask, what is this "Export for 4chan" thing?

Nacurutu avatar Feb 07 '23 16:02 Nacurutu

@Nacurutu It is an UI setting. If enabled, export every large/heavy image as a shrunk/smaller JPG. The setting has a "4MB" in its wording in the UI (sorry, I am not near a computer rn).

semjonsona avatar Feb 07 '23 18:02 semjonsona

@Nacurutu It is an UI setting. If enabled, export every large/heavy image as a shrunk/smaller JPG. The setting has a "4MB" in its wording in the UI (sorry, I am not near a computer rn).

thank you!

Nacurutu avatar Feb 07 '23 18:02 Nacurutu

https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/c429bcc5d8ec1710ef3f262a0acd6ec3bea9d474/modules/images.py#L553-L555 You could convert 16-bit to 8-bit before saving, like so:

elif extension.lower() in (".jpg", ".jpeg", ".webp"):
    if image_to_save.mode == 'RGBA':
        image_to_save = image_to_save.convert("RGB")
    elif image_to_save.mode == 'I;16':
        image_to_save = image_to_save.point(lambda p: p * 0.0038910505836576).convert("L")

missionfloyd avatar Feb 08 '23 04:02 missionfloyd

@missionfloyd Thanks for the .point(...) tip, good I now know that. I think to not use it here - I hardly can imagine anyone exporting 16 grayscale image and then complain why there was no version optimized for 4chan.

On another side, it could be very confusing if somebody writes code that should save 16 bit grayscale image as JPEG, but the quality of the saved images is worse than expected.

semjonsona avatar Feb 08 '23 08:02 semjonsona

JPEG doesn't support 16 bit, thus the error,

missionfloyd avatar Feb 08 '23 09:02 missionfloyd

related PR https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/7564 will have merge conflict if one gets merge first

w-e-w avatar Feb 08 '23 20:02 w-e-w

another related PR https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/7650

w-e-w avatar Feb 08 '23 20:02 w-e-w

@w-e-w ACK #7650. Will keep an eye on #7564 and rebase when it lands. Or maybe you could just take my patch and append it to your MR.

Another reason why not to try to convert 16bit monochrome JPEGs: https://github.com/python-pillow/Pillow/pull/3838#discussion_r292114051

semjonsona avatar Feb 09 '23 07:02 semjonsona

Or maybe you could just take my patch and append it to your MR.

yes I could, but I prefer to keep the issue separate

I think it's easier for AUTO to understand what's going on and if he does wish to merge your it is a trivial conflict resolve

there is also a chance that AUTO decides to take a different approach in that case it would definitely be better to keep them as separate PRs

w-e-w avatar Feb 10 '23 00:02 w-e-w

should be fixed by fb2354cb2ae47f9e9b70f0e04f34925bbb31b1ac

AUTOMATIC1111 avatar Feb 19 '23 07:02 AUTOMATIC1111