personal_site/assets/blog/alpha_test/white_to_alpha.py
Tom 37dbad1eed Squashed commit of the following:
commit 13bfa6c6f9e911420e83d2e051a2aa95857359d8
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Tue Mar 4 18:10:42 2025 +0100

    Finish post

commit 5ded765f53b3551ae8ddd466004ca7026c1a90f8
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Tue Mar 4 17:41:23 2025 +0100

    Writing post

commit 98487c174c8f9150f6e5e33922ee75460b100926
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Tue Mar 4 15:08:27 2025 +0000

    Quick fix

commit cadc04083d02e4e157f780d8a358e22e0c597b37
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Fri Feb 28 10:32:43 2025 +0000

    publish ecmwf wms post

commit 05b6311fd6e92e1d8fadd79c5651e06bbf2696c9
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Mon Feb 24 16:04:20 2025 +0000

    Update feed.xml

commit ac09cc1d39306de6226d66a72fc67c11aba4ad0f
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Thu Feb 20 08:54:57 2025 +0000

    Make bound states an animation

commit b16485972c81f3f0bd1dd9deda5ea3b114344f25
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Sun Feb 16 18:26:20 2025 +0000

    Squashed commit of the following:

    commit ddd4a9145fb903eba795f3dea9402268a099e651
    Author: Tom <thomas.hodson@ecmwf.int>
    Date:   Sun Feb 16 18:26:06 2025 +0000

        Make executable code snippets post

commit 00ff594495a1741d2666703367b10fd82e5c2048
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Sun Feb 16 17:36:09 2025 +0000

    Update new_post.py

commit ff965f1037e0a94d466dc51f5570f56f26990d2c
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Sun Feb 16 17:35:17 2025 +0000

    Update new_post.py

commit 5b3c86ee9a95d314286935ddc54ca0a306755280
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Sun Feb 16 17:32:29 2025 +0000

    Update new_post.py

commit 6f0d2a006abe8b9eea0a82b17425144e91e1965c
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Sun Feb 16 17:26:35 2025 +0000

    Don't open new posts in the browser just print the url

commit c05b03e8d16929d3a614a841919357c1a9eb427f
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Thu Feb 20 08:54:31 2025 +0000

    Update 2025-02-16-usb-c-psu-monitor-board-bringup.md

commit 718c56544a5ef3f49bfc86e1adab9fbf467111ff
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Sun Feb 16 21:02:35 2025 +0000

    Write post

commit 26de5b5515fec3fdb84fd397b34b6027c9dfee44
Author: Tom <thomas.hodson@ecmwf.int>
Date:   Sun Feb 16 17:26:14 2025 +0000

    Create 2025-02-16-usb-c-psu-monitor-board-bringup.md
2025-03-04 18:11:11 +01:00

39 lines
1.2 KiB
Python

#!/usr/bin/env python3
import sys
import numpy as np
from PIL import Image
if len(sys.argv) < 3:
print("Usage: python white_to_alpha.py <input_image_path> <output_image_path>")
sys.exit(1)
input_path, output_path = sys.argv[1], sys.argv[2]
# convert to 64bit floats from 0 - 1
d = np.asarray(Image.open(input_path).convert("RGBA")).astype(np.float64) / 255.0
print("Top left corner colour: ", d[0,0])
#decompose channels
# r,g,b,a = d.T
color = d[:, :, :3]
# The amount of white in each pixel
white = np.array([0.69803922, 0.69803922, 0.69803922])
white_amount = np.min(color / white, axis = 2)
alpha = 1 - white_amount
premultiplied_new_color = (color - (1 - alpha)[:, :, None] * white[None, None, :])
new_color = premultiplied_new_color / alpha[:, :, None]
original_color = alpha[:,:,None] * new_color + (1 - alpha[:,:,None]) * white
new_RGBA = np.concatenate([new_color, alpha[:,:,None]], axis = 2)
# Premultiplied alpha, but PIL doesn't seem to support it
# new_RGBa = np.concatenate([premultiplied_new_color, alpha[:,:,None]], axis = 2)
# print(np.info(new_RGBA))
img = Image.fromarray((new_RGBA * 255).astype(np.uint8), mode = "RGBA")
img.save(output_path)
print(f"Image saved to {output_path}")