将所有色盲类型的图像转换为色盲友好图像

2024-09-24 00:31:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个模块,用于将图像文件转换为可使用daltonize包查看各种色盲类型的版本

import numpy as np
from PIL import Image

# Using daltonize-0.1.0, see https://github.com/joergdietrich/daltonize
from daltonize import daltonize as dz 

def img2rgb(img_file):
    """Convert image to rgb array

    Parameters
    ----------
    img_file : str
        Filepath of png or jpg image

    Returns
    -------
    np.array
        np.array of shape (M, N, 3) representing rgb of image
    """
    return np.array(Image.open(img_file).convert('RGB'))

def colorblind_img(img_file, colorblind_type="d"):
    """Simulate the effect of color blindness on an image.
    (What a colorblind person sees the image as)

    Parameters
    ----------
    img_file : str
        Filepath of original image

    colorblind_type : One of {"d", "p", "t"}, optional
        type of colorblindness, d for deuteronopia (default),
        p for protonapia,
        t for tritanopia

    Returns
    -------
    PIL Image
        simulated image as a PIL Image
    """
    rgb_arr = img2rgb(img_file)
    cb_arr = dz.simulate(rgb_arr, color_deficit=colorblind_type).astype('uint8')
    cb_img = Image.fromarray(cb_arr)
    return cb_img

问题是,该函数一次只适用于一种色盲类型。如何制作一个函数,将图像转换为一个全面的色盲友好图像(适用于所有色盲类型)


Tags: ofimageimgastypenprgbarray