Python图像处理库PIL的Image模块介绍
PIL中所涉及的基本概念有如下几个:通道(bands)、模式(mode)、尺寸(size)、坐标系统(coordinate system)、调色板(palette)、信息(info)和滤波器(filters)。
1、 通道
每张图片都是由一个或者多个数据通道构成。PIL允许在单张图片中合成相同维数和深度的多个通道。
以RGB图像为例,每张图片都是由三个数据通道构成,分别为R、G和B通道。而对于灰度图像,则只有一个通道。
对于一张图片的通道数量和名称,可以通过方法getbands()来获取。方法getbands()是Image模块的方法,它会返回一个字符串元组(tuple)。该元组将包括每一个通道的名称。
Python的元组与列表类似,不同之处在于元组的元素不能修改,元组使用小括号,列表使用方括号,元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
方法getbands()的使用如下:
>>>from PIL import Image
>>> im= Image.open('D:\\Code\\Python\\test\\img\\1.jpg')
>>>im.getbands()
('R', 'G', 'B')
>>>im_bands = im.getbands()
>>>len(im_bands)
3
>>>print im_bands[0]
R
>>>print im_bands[1]
G
>>>print im_bands[2]
B
2、 模式
图像的模式定义了图像的类型和像素的位宽。当前支持如下模式:
1
:1位像素,表示黑和白,但是存储的时候每个像素存储为8bit。
L
:8位像素,表示黑和白。
P
:8位像素,使用调色板映射到其他模式。
RGB
:3x8位像素,为真彩色。
RGBA
:4x8位像素,有透明通道的真彩色。
CMYK
:4x8位像素,颜色分离。
YCbCr
:3x8位像素,彩色视频格式。
I
:32位整型像素。
F
:32位浮点型像素。
PIL也支持一些特殊的模式,包括RGBX(有padding的真彩色)和RGBa(有自左乘alpha的真彩色)。
可以通过mode属性读取图像的模式。其返回值是包括上述模式的字符串。
属性mode的使用如下:
>>> from PIL importImage
>>> im =Image.open('D:\\Code\\Python\\test\\img\\1.jpg')
>>> im.mode
'RGB'
>>> md = im.mode
>>> print md
RGB
3、 尺寸
通过size属性可以获取图片的尺寸。这是一个二元组,包含水平和垂直方向上的像素数。
属性mode的使用如下:
>>> from PIL importImage
>>> im =Image.open('D:\\Code\\Python\\test\\img\\1.jpg')
>>>im.size
(800, 450)
>>>im_size = im.size
>>>print im_size[0]
800
>>>print im_size[1]
450
4、 坐标系统
PIL使用笛卡尔像素坐标系统,坐标(0,0)位于左上角。注意:坐标值表示像素的角;位于坐标(0,0)处的像素的中心实际上位于(0.5,0.5)。
坐标经常用于二元组(x,y)。长方形则表示为四元组,前面是左上角坐标。例如,一个覆盖800x600的像素图像的长方形表示为(0,0,800,600)。
5、 调色板
调色板模式 (“P”)使用一个颜色调色板为每个像素定义具体的颜色值
————————————————
版权声明:本文为CSDN博主「icamera0」的原创文章
原文链接:https://blog.csdn.net/icamera0/article/details/50647465
下面接着说Palette
:
What is the difference between images in ‘P’ and ‘L’ mode in PIL?
- What are the difference between them?
- Can we convert from one to another?
Normally, images are RGB, which means they have 3 channels, one for red, one for green and one for blue. That normally means that each pixel takes 3 bytes of storage, one for red, one for green and one for blue.
If you have a P mode image, that means it is palettised. That means there is a palette with up to 256 different colours in it, and instead of storing 3 bytes for R, G and B for each pixel, you store 1 byte which is the index into the palette. This confers both advantages and disadvantages. The advantage is that your image requires 1/3 of the space in memory and on disk. The disadvantage is that it can only represent 256 unique colours - so you may get banding or artefacts.
If you have an L mode image, that means it is a single channel image - normally interpreted as greyscale. The L means that is just stores the Luminance. It is very compact, but only stores a greyscale, not colour.
You convert between them using the convert(mode)
function, e.g. to go to RGB mode, use:
image.convert('RGB')
I used the word “normally” quite a lot! Why? Because you can do abnormal things!
-
You can store a grey-looking image in an RGB format. All you do, is make the red component equal to the green component equal to the blue component (R=G=B) and it will appear grey but be stored in an inefficient RGB format that takes 3x the space it might otherwise need to.
-
You can store a grey-looking image in a P format, you just make sure all the palette entries have the R=G=B.
Here’s the kicker… if you want and expect an RGB image, you should just convert to RGB on opening:
im = Image.open("image.jpg").convert('RGB')
that way you will never have problems with GIF files (which are always palettised) nor with PNG files which can be palettised and can be greyscale or RGB. You will not normally get problems with JPEG images because they are pretty much always RGB anyway.
以上Content of English来自:
https://stackoverflow.com/questions/52307290/what-is-the-difference-between-images-in-p-and-l-mode-in-pil
未完待续~~~~~~