ChromaLensCLI: Command Line Color Accessibility Validation
デザイン入門のようなコースをうけていて「色覚異常の方もいるので気をつけましょうね」とか説明されてんだけど、そんな適当な説明じゃなくちゃんとAccessibilityについて説明しろよと思う。だいたい「気をつける」のでなく、世の中にはちゃんとチェックするツールもあるし・・・・ とりあえず色覚異常の方にも画像が正しく認識できてるか確認するためのツール 使い方は、次の通り。 これで色覚異常の方が実際どのようにインプットとなる画像が見えているかシミュ レーションした画像が出力されます。 以下は、venvでPythonの仮想環境を作って必要なパッケージをインストールする手順です。 以下が実際のコードです。ChromaLens CLIを作ってみた。ChromaLens CLIの使い方
./chromalens input.jpg output.jpg --type tritanopiatype オプションの指定は次の通りです。ChromeLens CLIの環境設定
python3 -m venv venv
source venv/bin/activate.fish
pip install numpy Pillowchromalensというファイル名で保管ください。授業中に書いたコードなので、エラー処理などは省いています。#!/usr/bin/env python3
from PIL import Image
class ColorVisionSimulator:
MATRICES = {
'protanopia': np.([
[0.567, 0.433, 0.000],
[0.558, 0.442, 0.000],
[0.000, 0.242, 0.758]
]),
'deuteranopia': np.([
[0.625, 0.375, 0.000],
[0.700, 0.300, 0.000],
[0.000, 0.300, 0.700]
]),
'tritanopia': np.([
[0.950, 0.050, 0.000],
[0.000, 0.433, 0.567],
[0.000, 0.475, 0.525]
])
}
def simulate(self, image_path, output_path, cvd_type):
if cvd_type not in self.MATRICES:
raise ValueError(f"Unsupported CVD type. Choose from: {', '.(self..())}")
# 画像を読み込み
img = Image.()
# RGBAの場合はRGBに変換
if img.mode == 'RGBA':
img = img.('RGB')
# NumPy配列に変換
img_array = np.()
# 色変換行列を適用
matrix = self.MATRICES[cvd_type]
shape = img_array.shape
pixels = img_array.(-1, 3)
converted = np.(,.)
# 値を0-255の範囲に収める
converted = np.(, 0, 255)
converted = converted.()
# 画像として保存
result = Image.(.('uint8'), 'RGB')
result.()
def main():
parser = argparse.(description='Color Vision Deficiency Simulator')
parser.('input', help='Input image path')
parser.('output', help='Output image path')
parser.('--type', '-t', choices=['protanopia', 'deuteranopia', 'tritanopia'],
required=True, help='Type of color vision deficiency')
args = parser.()
simulator =()
try:
simulator.(.,.,.)
print(f"Simulation complete: {.}")
except Exception as e:
print(f"Error: {str()}")
exit(1)
if __name__ == '__main__':
()