From 6fe7e5e5a6a5d37518aa2b85c2a3280a05efdde0 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 24 Jun 2023 23:59:12 -0700 Subject: [PATCH] Add function convert_to_srgb. --- voussoirkit/imagetools.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/voussoirkit/imagetools.py b/voussoirkit/imagetools.py index efa3c66..e022a90 100644 --- a/voussoirkit/imagetools.py +++ b/voussoirkit/imagetools.py @@ -5,6 +5,7 @@ import exifread import io import PIL.ExifTags import PIL.Image +import PIL.ImageCms import re from voussoirkit import pathclass @@ -40,6 +41,20 @@ def checkerboard_image(color_1, color_2, image_size, checker_size) -> PIL.Image: offset = not offset return image +def convert_to_srgb(image): + ''' + Thank you Andriy Makukha + https://stackoverflow.com/a/50623824 + ''' + icc = image.info.get('icc_profile', '') + if icc: + image = PIL.ImageCms.profileToProfile( + image, + inputProfile=PIL.ImageCms.ImageCmsProfile(io.BytesIO(icc)), + outputProfile=PIL.ImageCms.createProfile('sRGB'), + ) + return image + def fit_into_bounds( image_width, image_height,