Skip to content

Instantly share code, notes, and snippets.

@plouvart
Last active April 20, 2023 12:01
Show Gist options
  • Save plouvart/3cdbd4f72c789e42a286a7eb93edbebc to your computer and use it in GitHub Desktop.
Save plouvart/3cdbd4f72c789e42a286a7eb93edbebc to your computer and use it in GitHub Desktop.
Transfert the colorimetry from one image to another
from PIL import Image
from pathlib import Path
import numpy as np
import sys
def adjust(
in_f1: Path,
in_f2: Path,
out_f: Path,
) -> None:
"""Adjust colorimetry
Rough colorimetry transfer from
jpg image in_f2 to jpg image in_f1.
Result saved into jpg image out_f.
"""
i1 = np.array(Image.open(in_f1))
i2 = np.array(Image.open(in_f2))
Image.fromarray(
(
(i1 - i1.mean(axis=(0,1))) / i1.std(axis=(0,1))
* i2.std(axis=(0,1)) + i2.mean(axis=(0,1))
).astype(np.uint8)
).save(out_f)
if __name__ == "__main__":
adjust(
in_f1 = Path(sys.argv[1]),
in_f2 = Path(sys.argv[2]),
out_f = Path(sys.argv[3]),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment