From b9ddf0ce37733e2168c81e1451077aa50962c021 Mon Sep 17 00:00:00 2001 From: JM Date: Sun, 28 Sep 2025 15:10:09 +0300 Subject: [PATCH] works without ImageCms --- library/train_util.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/library/train_util.py b/library/train_util.py index 0d294e7f..c1b02b8c 100644 --- a/library/train_util.py +++ b/library/train_util.py @@ -66,7 +66,12 @@ from library import custom_train_functions from library.original_unet import UNet2DConditionModel from huggingface_hub import hf_hub_download import numpy as np -from PIL import Image, ImageCms +import sys +from PIL import Image +try: + from PIL import ImageCms +except: + print( "ImageCms not available. Images will not be converted to sRGB. Colours may be handled incorrectly." ) import imagesize import cv2 import safetensors.torch @@ -2494,15 +2499,16 @@ def load_image(image_path, alpha=False): raise Exception( f"{image_path} is animated" ) # Convert image to sRGB - icc = image.info.get('icc_profile', '') - if icc: - try: - src_profile = ImageCms.ImageCmsProfile( BytesIO(icc) ) - srgb_profile = ImageCms.createProfile("sRGB") - ImageCms.profileToProfile(image, src_profile, srgb_profile, inPlace=True) - image.info["icc_profile"] = ImageCms.ImageCmsProfile(srgb_profile).tobytes() - except Exception as e: - raise Exception( f"Could not convert {image_path} to sRGB: {src_profile.profile.model} {src_profile.profile.profile_description}\n{e}" ) + if "PIL.ImageCms" in sys.modules: + icc = image.info.get('icc_profile', '') + if icc: + try: + src_profile = ImageCms.ImageCmsProfile( BytesIO(icc) ) + srgb_profile = ImageCms.createProfile("sRGB") + ImageCms.profileToProfile(image, src_profile, srgb_profile, inPlace=True) + image.info["icc_profile"] = ImageCms.ImageCmsProfile(srgb_profile).tobytes() + except Exception as e: + raise Exception( f"Could not convert {image_path} to sRGB: {src_profile.profile.model} {src_profile.profile.profile_description}\n{e}" ) if alpha: if not image.mode == "RGBA":