PS 中 图像->黑白 算法实现(python版)

PS 中 图像->黑白 算法实现(python版)


详细内容就不解释了,代码有注释, 另外在网上看了很多代码,很多版本,就不做评价了。给自己留个记录的位置和有需要的有缘人。

示例图:

#!/usr/bin/python3
# coding: utf-8
# @Time: 2021/1/15 13:11
# @Project: ObjectDetection
# @File: BlackWhite.py
# @Author: Lpliner
# @Description:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io


class BlackWhite(object):

    def __init__(self, filename, red=.4, yellow=.6, green=.4, cyan=.6, blue=.2, magenta=.8):
        """
        ps中图像->黑白: python版, 及各种颜色在灰度图片中的占比
        红色, 黄色, 绿色, 青色, 蓝色, 洋红
        Args:
            filename: 修改的文件路径,abspath
            red:  红色占比,参数范围[-2.0, 3.0]
            yellow: 黄红色占比,参数范围[-2.0, 3.0]
            green: 绿红色占比,参数范围[-2.0, 3.0]
            cyan: 青红色占比,参数范围[-2.0, 3.0]
            blue: 蓝色占比,参数范围[-2.0, 3.0]
            magenta: 洋红色占比,参数范围[-2.0, 3.0]
        """
        self.filename = filename
        self.red = red
        self.yellow = yellow
        self.green = green
        self.cyan = cyan
        self.blue = blue
        self.magenta = magenta

    def __image_read(self, cv=True):
        """
        read image file
        Returns:

        """
        if cv:
            # opencv
            img = cv2.imread(self.filename)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        else:
            # scikit-image
            img = io.imread(self.filename)
        return img

    def __transform(self):
        """
        调整图像数据黑白度
        Returns:

        """
        image = self.__image_read()
        image = image * 1.0  # 将int数据类型转换成float

        color_ratio = np.array([self.red, self.yellow, self.green, self.cyan, self.blue, self.magenta])

        # 读取到的image时三维数据,所以取值为axis=2
        image_max_value = image.max(axis=2)
        image_min_value = image.min(axis=2)
        image_sum_value = image.sum(axis=2)
        image_mid_value = image_sum_value - image_max_value - image_min_value

        # 添加蒙版ratio_max_mid
        mask_red = 1 - ((image[:, :, 0] - image_max_value + 0.01) < 0)
        mask_green = 1 - ((image[:, :, 1] - image_max_value + 0.01) < 0)
        mask_blue = 1 - ((image[:, :, 2] - image_max_value + 0.01) < 0)

        ratio_max_mid = mask_red * color_ratio[3] + mask_green * color_ratio[5] + mask_blue * color_ratio[1]

        # 添加蒙版ratio_max
        mask_red = 1 - ((image[:, :, 0] - image_min_value - 0.01) > 0)
        mask_green = 1 - ((image[:, :, 1] - image_min_value - 0.01) > 0)
        mask_blue = 1 - ((image[:, :, 2] - image_min_value - 0.01) > 0)

        ratio_max = mask_red * color_ratio[4] + mask_green * color_ratio[0] + mask_blue * color_ratio[2]

        # output = image_max_value * 1.0

        # gray algorithm
        # gray = (max - mid) * ratio_max + (mid - min) * ratio_max_mid + min
        output = (image_max_value - image_mid_value) * ratio_max + (
                    image_mid_value - image_min_value) * ratio_max_mid + image_min_value

        return output

    def save(self, image, filename):
        """
        将图片数据另存为目标地址
        Args:
            image: 图片数据np.ndarray
            filename: 存为文件位置

        Returns:

        """
        cv2.imwrite(filename, image)

    def show(self, image):
        """
        显示图片, 此处使用plt绘图,可以更换opencv展示
        Args:
            image: 图片数据

        Returns:

        """
        plt.figure(2)
        plt.imshow(image / 255.0, plt.cm.gray)
        plt.axis('off')

        plt.show()

    def __call__(self, *args, **kwargs):
        image = self.__transform()
        # self.save(image, kwargs.get("saveDir"))
        self.show(image)


if __name__ == '__main__':
    filename = "./test1.jpg"
    BlackWhite(filename,
               red=2.,
               yellow=-1.,
               green=-1.,
               cyan=-1.,
               blue=-1.,
               magenta=1.
               )()

你可能感兴趣的:(学习,python,算法,photoshop)