两种图像透明背景转特定颜色方法的比较

之前写过一篇博客,关于透明背景转换为特定颜色,当时使用了NumPy数组采用布尔索引转换的方式,这次我们把这种转换和常规的逐像素转换的方式进行比较,看那种方法效率更高。记得以前使用Matlab的时候,显然是矩阵布尔索引的方式会有更高的效率,不知道Python是否也一样。

  1. 代码实现:
python 复制代码
def transparent2bgra_color1(img, bgra_color=(255, 255, 255, 255)):
    """
    将图像中的透明区域替换为指定的BGRA颜色。
    
    参数:
    img: 输入的BGRA格式图像,其中A通道为透明度。
    bgra_color: 替换透明区域的颜色,默认为白色(255, 255, 255, 255)。
    
    返回:
    返回替换透明区域后的图像。
    """
    # 为了避免原图像发生改变,创建一个副本进行操作
    res = img.copy()
    # 将图像中透明度为0的区域替换为指定的BGRA颜色
    res[img[:, :, 3] == 0] = bgra_color
    return res

def transparent2bgra_color2(src, bgra_color=(255, 255, 255, 255)):
    """
    将图片中的透明部分替换为指定的BGRA颜色。
    
    参数:
    src: 输入的图片,应为BGRA格式。
    bgra_color: 替换透明部分的颜色,默认为白色(255, 255, 255, 255)。
    
    返回:
    替换透明部分后的图片。
    """
    # 复制输入图片,避免修改原图
    img=src.copy()
    # 获取图片的宽度和高度
    sp=img.shape  
    width=sp[0]  
    height=sp[1]  
    # 遍历图片的每个像素点
    for yh in range(height):
        for xw in range(width):
            # 获取当前点的颜色数据
            color_d=img[xw,yh]  
            # 检查当前点的透明度,如果完全透明,则替换颜色
            if(color_d[3]==0):  
                img[xw,yh]=bgra_color  
    # 返回处理后的图片
    return img
  1. 效率对比测试

使用Benchmark,我们可以测试这两种方法的性能,代码如下:

python 复制代码
import pytest
import cv2

def transparent2bgra_color1(img, bgra_color=(255, 255, 255, 255)):
    """
    将图像中的透明区域替换为指定的BGRA颜色。
    
    参数:
    img: 输入的BGRA格式图像,其中A通道为透明度。
    bgra_color: 替换透明区域的颜色,默认为白色(255, 255, 255, 255)。
    
    返回:
    返回替换透明区域后的图像。
    """
    # 为了避免原图像发生改变,创建一个副本进行操作
    res = img.copy()
    # 将图像中透明度为0的区域替换为指定的BGRA颜色
    res[img[:, :, 3] == 0] = bgra_color
    return res

def transparent2bgra_color2(src, bgra_color=(255, 255, 255, 255)):
    """
    将图片中的透明部分替换为指定的BGRA颜色。
    
    参数:
    src: 输入的图片,应为BGRA格式。
    bgra_color: 替换透明部分的颜色,默认为白色(255, 255, 255, 255)。
    
    返回:
    替换透明部分后的图片。
    """
    # 复制输入图片,避免修改原图
    img=src.copy()
    # 获取图片的宽度和高度
    sp=img.shape  
    width=sp[0]  
    height=sp[1]  
    # 遍历图片的每个像素点
    for yh in range(height):
        for xw in range(width):
            # 获取当前点的颜色数据
            color_d=img[xw,yh]  
            # 检查当前点的透明度,如果完全透明,则替换颜色
            if(color_d[3]==0):  
                img[xw,yh]=bgra_color  
    # 返回处理后的图片
    return img

img = cv2.imread('dog.png', cv2.IMREAD_UNCHANGED)

@pytest.mark.parametrize("input_data", [img])
def test_trans_color1(benchmark, input_data):
    res = benchmark(transparent2bgra_color1, input_data)
    assert (667, 1000, 4) == res.shape

@pytest.mark.parametrize("input_data", [img])
def test_trans_color2(benchmark, input_data):
    res = benchmark(transparent2bgra_color2, input_data)
    assert (667, 1000, 4) == res.shape    
  1. 结果

显然,Python也与Matlab类似,在数组(包括矩阵)的运算当中,布尔索引的方式具有更高的执行效率。

相关推荐
北京_宏哥13 分钟前
🔥PC端自动化测试实战教程-2-pywinauto 启动PC端应用程序 - 上篇(详细教程)
前端·windows·python
_x_w22 分钟前
【12】数据结构之基于线性表的排序算法
开发语言·数据结构·笔记·python·算法·链表·排序算法
北京_宏哥24 分钟前
🔥PC端自动化测试实战教程-1-pywinauto 环境搭建(详细教程)
前端·windows·python
grrrr_135 分钟前
【ctfplus】python靶场记录-任意文件读取+tornado模板注入+yaml反序列化(新手向)
python·web安全·tornado
ππ记录1 小时前
python面试技巧
python·python面试·python面试技巧·python面试能力
csdn_aspnet1 小时前
windows 安装 pygame( pycharm)
python·pycharm·pygame
这里有鱼汤1 小时前
Python自动化神器Playwright:让浏览器乖乖听话的终极指南!
后端·爬虫·python
海天一色y1 小时前
Pycharm(十三)容器类型的公共运算符和公共方法
ide·python·pycharm
这里有鱼汤2 小时前
Python多线程编程,你必须掌握的并发编程技巧!
python
q567315232 小时前
用Dispatch库的爬虫程序爬取图片网站
开发语言·爬虫·python·scrapy