Python PIL Image 4通道透明图片叠加(ARGB) 您所在的位置:网站首页 image/4/step6swap4.gif Python PIL Image 4通道透明图片叠加(ARGB)

Python PIL Image 4通道透明图片叠加(ARGB)

2023-07-20 18:35| 来源: 网络整理| 查看: 265

安装Package pip install Pillow pip install numpy RGBA与RGB的区别

一般图片(Jpeg)的呈现都是以3通道RGB显示出来

from PIL import Image import numpy as np img = Image.open('test.jpg') print(img .mode) array = np.array(img ) print(array.shape)

输出为【RGB】,【(2048, 2048, 3)】

但是png格式的图片可以加上Alpha通道表示透明层,其实可以理解成以GrayScale level 表示透明度,其Alpha取值【0,255】, 0表示为透明最大化,255则为不透明最大化。

img = Image.open('test.png') print(img.mode) array = np.array(img) print(array.shape)

输出为【RGBA】,【(2048, 2048, 4)】

PIL Image 4通道透明图片叠加

图片的叠加可以通过PIL Image 的paste函数进行

Image.paste(im, box=None, mask=None)

im = 叠加的图片

box = 叠加的坐标,默认坐标为左上角

mask = 叠加图片的mask

以下测试图为 左Jpeg, 右为4通道Png

一般叠加图片来说直接使用Image.paste(im)就可以了,但是这样的做法的会忽略了Alpha通道。

from PIL import Image bg = Image.open('1.jpg') # background image layer = Image.open('2.png') # 4 channel png image bg.paste(layer) bg.show()

 处理Alpha channel 透明层的秘诀就是在于Image.paste(im)加上mask。

from PIL import Image bg = Image.open('1.jpg') # background image layer = Image.open('2.png') # 4 channel png image bg = bg.resize((2048,2048)) bg.paste(layer, (0,0), layer) bg.show()

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有