Add a script for srgbify
- ID
b3c4fc3- date
2023-10-28 22:44:30+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
3089736- message
Add a script for srgbify- changed files
2 files, 46 additions
Changed files
images/README.md (5566) → images/README.md (5782)
diff --git a/images/README.md b/images/README.md
index d4daaee..aaa179e 100644
--- a/images/README.md
+++ b/images/README.md
@@ -158,6 +158,15 @@ These scripts are for working with images and other visual material.
<dd>
saves a single comic from <a href="https://xkcd.com/">xkcd</a>, plus some metadata.
</dd>
+
+ <dt>
+ <a href="https://github.com/alexwlchan/scripts/blob/main/images/srgbify">
+ <code>srgbify [PATH]</code>
+ </a>
+ </dt>
+ <dd>
+ convert an image in-place to have an sRGB colour profile.
+ </dd>
<dt>
<a href="https://github.com/alexwlchan/scripts/blob/main/images/tint_image">
images/srgbify (0) → images/srgbify (935)
diff --git a/images/srgbify b/images/srgbify
new file mode 100755
index 0000000..7eb2c95
--- /dev/null
+++ b/images/srgbify
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+"""
+A script that converts images to an sRGB colour profile.
+
+This is particularly useful for screenshots on macOS, which are taken
+with the display's colour profile (e.g. Display LCD or Display P3), but
+which I want to convert to sRGB for converting on the web.
+
+Based on https://github.com/python-pillow/Pillow/issues/1662
+"""
+
+import os
+import sys
+import tempfile
+
+from PIL import Image, ImageCms
+
+
+if __name__ == "__main__":
+ try:
+ path = sys.argv[1]
+ except IndexError:
+ sys.exit(f"Usage: {__file__} <PATH>")
+
+ in_img = Image.open(path)
+
+ srgb_profile = ImageCms.createProfile("sRGB")
+
+ _, icc_profile = tempfile.mkstemp(suffix=".icc")
+ with open(icc_profile, "wb") as out_file:
+ out_file.write(in_img.info.get("icc_profile"))
+
+ out_img = ImageCms.profileToProfile(
+ in_img, inputProfile=icc_profile, outputProfile=srgb_profile
+ )
+
+ out_img.save(path)