PIL库 (Pillow)

时间:2022-06-04 09:38:44

PIL基础

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

PIL (Pillow)

Pillow 是 PIL的对Python3支持的另外一个分支,当然他对Python2也兼容,由于PIL安装起来比较烦,而使用pip可以很轻松的安装Pillow,所以我选择Pillow使用,但是其核心还是PIL库的。


Python的图形处理库如PIL一直很强大,但是要想使用好它必须对图片有一定的知识储备。 使用起来很简单

    from PIL import Image

引用Image包

    im = Image.open('1.png')

打开图片,得到一个im对象,我们接下来就可以对这个对象进行操作(前提有这个1.png图片)

我们先看一下他的一些属性

    >>> print im.format, im.size, im.mode
PNG (83, 81) RGB

第一个我们输出图片的格式,图片有很多种格式,常用的有jpg、png还有gif动图啊,PIL支持很多种格式,我们可以使用PIL轻松的将格式转换,im.save('1.jpg'),当然你可以选择格式假如你没选好后缀名的话,im.size就是图片大小,他返回的是一个元组第一个长度第二个是宽度,单位是像素。 现在就谈谈 这三个属性对应的关系吧 首先我们使用一张像素图来说吧

PIL库 (Pillow)

我们存贮图片的时候是将整个图像分成很多个相同的小方块,每个小方块我们称为像素,当然一张图片分的越小,像素越多,那么图片就越接近真实图片,上面的im.size属性就告诉我们,这张图片分成了,长为83px,宽为81px的图片,那么一共有83*81=6723个像素点,每个像素点里面存什么呢,这就是im.mode属性告诉我们的,贴一下属性有什么吧

  1. 1 (1-bit pixels, black and white, stored with one pixel per byte)
  2. L (8-bit pixels, black and white)
  3. P (8-bit pixels, mapped to any other mode using a color palette)
  4. RGB (3x8-bit pixels, true color)
  5. RGBA (4x8-bit pixels, true color with transparency mask)
  6. CMYK (4x8-bit pixels, color separation)
  7. YCbCr (3x8-bit pixels, color video format)
  8. I (32-bit signed integer pixels)
  9. F (32-bit floating point pixels)

像素存贮就是涉及到颜色的存贮,在早期的黑白游戏机,只有黑和白两种,那么每个像素点就只有1位颜色来存贮,1位只能存贮两种颜色,八位色就能存256种颜色,像八位我们能用256个油漆桶/256色调色板来形容,像上面我们使用的RGB是由三种三原色红绿蓝混合而成,我们知道大自然所有的颜色都可以用红绿蓝三种颜色调配出来,所以RGB又被称为真彩(true color),每种颜色我们都分成256种,所以我们一共有256256256=16777216种颜色可以调配,像素的其他模式我们不介绍太多,有兴趣的可以自己钻研。 那么我们知道每个像素占多少字节,又知道共有多少个像素,那我们是不是就可以直接计算出来图片大小,来验证一下

以第一张图片为例,共有8381=6723个像素点,用RGB模式,每个像素三个字节,共有67233=20667b=20kb,但是我这张图片只有11.6kb,误差太大了吧,这时候我们就要介绍一下上面那个im.format属性了,这张图片采用png格式,我们先尝试一下把他转成JPG格式吧

im.save('1.jpg')

我们再查看一下这个1.jpg的大小,只有2.24kb了,我们用PIL打开这张图片

    >>> im2 = Image.open('1.jpg')
>>> print im2.format, im2.size, im2.mode
JPEG (83, 81) RGB

图片大小没有改变,但是format变成了JPEG,而且文件大小变成原来的1/5, JPEG和GIF和PNG是三种图片压缩技术,他们使用压缩算法把图片压缩成很小,当我们打开图片时,解密算法把他还原出来,所以我们算出来的大小与压缩后的大小是不一样的。 有了这些概念我们就能更好的使用PIL提供给我们的magic方法,下次在谈我对PIL的高级应用吧。