Okay, to apply this ICC profile to a bitmap image, you'll primarily use the Pillow (PIL Fork) library in Python, specifically its ImageCms module, which leverages Little CMS (LCMS).
Here's how you can do it:
-
Install Pillow:
pip install Pillow
-
Python Code:
from PIL import Image, ImageCms
import io
# The ICC profile data you provided
profile_data_hex = b'\x00\x00\x02\x18\x00\x00\x00\x00\x040\x00\x00mntrRGB XYZ \x07\xe0\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00acsp\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\xf6\xd6\x00\x01\x00\x00\x00\x00\xd3-\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\tdesc\x00\x00\x00\xf0\x00\x00\x00trXYZ\x00\x00\x01d\x00\x00\x00\x14gXYZ\x00\x00\x01x\x00\x00\x00\x14bXYZ\x00\x00\x01\x8c\x00\x00\x00\x14wtpt\x00\x00\x01\xa0\x00\x00\x00\x14rTRC\x00\x00\x01\xb4\x00\x00\x00(gTRC\x00\x00\x01\xb4\x00\x00\x00(bTRC\x00\x00\x01\xb4\x00\x00\x00(cprt\x00\x00\x01\xdc\x00\x00\x00<mluc\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x0cenUS\x00\x00\x00X\x00\x00\x00\x1c\x00G\x00o\x00o\x00g\x00l\x00e\x00/\x00S\x00k\x00i\x00a\x00/\x007\x00C\x005\x00F\x00A\x002\x001\x005\x001\x003\x009\x007\x004\x007\x004\x00A\x000\x004\x008\x006\x00B\x00B\x00C\x00C\x008\x003\x007\x003\x003\x00D\x005\x009XYZ \x00\x00\x00\x00\x00\x00o\xa2\x00\x008\xf5\x00\x00\x03\x90XYZ \x00\x00\x00\x00\x00\x00b\x99\x00\x00\xb7\x85\x00\x00\x18\xdaXYZ \x00\x00\x00\x00\x00\x00$\xa0\x00\x00\x0f\x84\x00\x00\xb6\xcfXYZ \x00\x00\x00\x00\x00\x00\xf6\xd6\x00\x01\x00\x00\x00\x00\xd3-para\x00\x00\x00\x00\x00\x04\x00\x00\x00\x02ff\x00\x00\xf2\xa7\x00\x00\rY\x00\x00\x13\xd0\x00\x00\n[\x00\x00\x00\x00\x00\x00\x00\x00mluc\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x0cenUS\x00\x00\x00 \x00\x00\x00\x1c\x00G\x00o\x00o\x00g\x00l\x00e\x00 \x00I\x00n\x00c\x00.\x00 \x002\x000\x001\x006'
def apply_icc_profile_to_image(input_image_path, output_image_path, target_icc_profile_bytes):
"""
Applies a given ICC profile to an image.
Args:
input_image_path (str): Path to the input image file.
output_image_path (str): Path to save the transformed image.
target_icc_profile_bytes (bytes): The ICC profile data as bytes.
"""
try:
# 1. Load the input image
img = Image.open(input_image_path)
# Ensure image is in RGB mode for most color profile operations
# (unless your profile is specifically for CMYK, Grayscale, etc.)
# The provided profile is 'mntrRGB', so RGB is correct.
if img.mode not in ['RGB', 'RGBA']:
print(f"Converting image from {img.mode} to RGB before applying profile.")
img = img.convert('RGB')
elif img.mode == 'RGBA':
print("Image is RGBA. Alpha channel will be preserved but not color managed by this RGB profile.")
# Keep it as RGBA for now, ImageCms will handle the RGB part
pass
# 2. Get the source (input) profile
# Try to get embedded profile from the image
input_profile_bytes = img.info.get('icc_profile')
if input_profile_bytes:
print("Found embedded ICC profile in the input image.")
src_profile_handle = io.BytesIO(input_profile_bytes)
else:
# If no profile is embedded, a common assumption for RGB images is sRGB.
# You might need to make a different assumption based on your image source.
print("No embedded ICC profile found in input image. Assuming sRGB as source.")
# For other common source profiles, you might use:
# src_profile_handle = ImageCms.createProfile("AdobeRGB1998")
# src_profile_handle = ImageCms.createProfile("ProPhotoRGB")
src_profile_handle = ImageCms.createProfile("sRGB") # This returns a profile object directly
# 3. Load the target ICC profile (the one you provided)
# We need to wrap the byte string in a file-like object for ImageCms
target_profile_handle = io.BytesIO(target_icc_profile_bytes)
# 4. Perform the color transformation
print(f"Applying target profile to image (mode: {img.mode}).")
# Determine rendering intent.
# Common intents:
# - ImageCms.INTENT_PERCEPTUAL (good for photos, preserves visual relationship)
# - ImageCms.INTENT_RELATIVE_COLORIMETRIC (good for logos/spot colors, maps white point)
# - ImageCms.INTENT_SATURATION (maximizes saturation, good for charts)
# - ImageCms.INTENT_ABSOLUTE_COLORIMETRIC (simulates paper white of source)
intent = ImageCms.INTENT_PERCEPTUAL
# Create the transform.
# Note: If src_profile_handle is already a profile object (like from createProfile),
# you don't need ImageCms.getOpenProfile for it.
# If it's a BytesIO object (from embedded profile or your target profile),
# then ImageCms.getOpenProfile is needed.
if isinstance(src_profile_handle, io.BytesIO):
source_profile_obj = ImageCms.getOpenProfile(src_profile_handle)
else: # It's already a profile object from ImageCms.createProfile
source_profile_obj = src_profile_handle
target_profile_obj = ImageCms.getOpenProfile(target_profile_handle)
# It's crucial that the input and output modes for the transform match the
# color space of the profiles and the image data being transformed.
# Since your target profile is 'mntrRGB', we expect 'RGB' data.
# If img.mode is 'RGBA', ImageCms.profileToProfile or applyTransform
# will generally operate on the RGB channels.
transform_input_mode = 'RGB' if img.mode == 'RGBA' else img.mode
# The output mode of the transformation should match the target profile's color space
transform_output_mode = 'RGB' # As per 'mntrRGB' in your profile
# If your image has an alpha channel (RGBA), you need to handle it.
# One way is to separate alpha, transform RGB, then recombine.
# Pillow's ImageCms.profileToProfile or applyTransform can sometimes handle this
# gracefully by transforming only the color channels.
# Let's test the direct approach first.
if img.mode == 'RGBA':
# Separate alpha channel
alpha = img.split()[-1]
rgb_img = img.convert('RGB') # Work on the RGB part
transformed_rgb_img = ImageCms.profileToProfile(rgb_img,
source_profile_obj,
target_profile_obj,
renderingIntent=intent,
outputMode=transform_output_mode)
# Recombine with alpha
# Ensure transformed image is in a mode that can accept an alpha channel
if transformed_rgb_img.mode != 'RGB':
transformed_rgb_img = transformed_rgb_img.convert('RGB')
transformed_img = Image.merge('RGBA', (*transformed_rgb_img.split(), alpha))
else: # For RGB or other modes directly handled
transformed_img = ImageCms.profileToProfile(img,
source_profile_obj,
target_profile_obj,
renderingIntent=intent,
outputMode=transform_output_mode)
# 5. Save the transformed image, embedding the new (target) profile
print(f"Saving transformed image to {output_image_path}")
transformed_img.save(output_image_path, icc_profile=target_icc_profile_bytes)
print("Done.")
except FileNotFoundError:
print(f"Error: Input image file not found at '{input_image_path}'")
except ImageCms.PyCMSError as e:
print(f"Pillow CMS Error: {e}")
print("This could be due to an invalid profile, incompatible image mode, or other CMS issues.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# --- Example Usage ---
if __name__ == "__main__":
# Create a dummy input image for testing if you don't have one
# This will be an RGB image without an embedded profile (common scenario)
try:
dummy_input_path = "input_test_image.png"
img_test = Image.new("RGB", (200, 200), "blue")
# Add some red to see a change
from PIL import ImageDraw
draw = ImageDraw.Draw(img_test)
draw.rectangle([50, 50, 150, 150], fill="red")
img_test.save(dummy_input_path)
print(f"Created dummy input image: {dummy_input_path}")
input_image_file = dummy_input_path # Or "your_image.jpg", "your_image.png", etc.
output_image_file = "output_image_with_google_skia_profile.png"
apply_icc_profile_to_image(input_image_file, output_image_file, profile_data_hex)
print(f"\nCheck '{output_image_file}'.")
print("You can verify the embedded profile using a tool like ImageMagick's 'identify -verbose'")
print("or by opening it in a color-managed application (e.g., GIMP, Photoshop, Krita).")
print("The visual difference might be subtle or significant depending on the source and target profiles.")
except ImportError:
print("Pillow is not installed. Please install it using 'pip install Pillow'")
except Exception as e:
print(f"Error during example setup or execution: {e}")