python pillow模块用法
<h2 id="pillow"><strong>pillow</strong></h2><p>Pillow是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库。pillow可以说已经取代了PIL,将其封装成python的库(pip即可安装),且支持python2和python3,目前最新版本是3.0.0。</p>
<p>Pillow的Github主页:https://github.com/python-pillow/Pillow <br>Pillow的文档(对应版本v3.0.0): <br>https://pillow.readthedocs.org/en/latest/handbook/index.html</p>
<p>安装它很简单 <code>pip install pillow</code></p>
<p>使用方式:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">python2 </span>
<span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Image
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">python3(因为是派生的PIL库,所以要导入PIL中的Image) </span>
<span style="color: rgba(0, 0, 255, 1)">from</span> PIL <span style="color: rgba(0, 0, 255, 1)">import</span> Image</pre>
</div>
<p> </p>
<p><strong>以python3为例,</strong></p>
<ul>
<li>open</li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> PIL <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Image
im </span>= Image.open(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1.png</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
im.show()</span></pre>
</div>
<p> </p>
<ul>
<li>format</li>
</ul>
<blockquote>
<p>format属性定义了图像的格式,如果图像不是从文件打开的,那么该属性值为None;size属性是一个tuple,表示图像的宽和高(单位为像素);mode属性为表示图像的模式,常用的模式为:L为灰度图,RGB为真彩色,CMYK为pre-press图像。如果文件不能打开,则抛出IOError异常。</p>
</blockquote>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span>(im.format, im.size, im.mode)</pre>
</div>
<p> </p>
<ul>
<li>save</li>
</ul>
<div class="cnblogs_code">
<pre>im.save(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">c:\\</span><span style="color: rgba(128, 0, 0, 1)">"</span>)</pre>
</div>
<ul>
<li>convert()</li>
</ul>
<blockquote>
<p>convert() 是图像实例对象的一个方法,接受一个 mode 参数,用以指定一种色彩模式,mode 的取值可以是如下几种: <br>· 1 (1-bit pixels, black and white, stored with one pixel per byte) <br>· L (8-bit pixels, black and white) <br>· P (8-bit pixels, mapped to any other mode using a colour palette) <br>· RGB (3x8-bit pixels, true colour) <br>· RGBA (4x8-bit pixels, true colour with transparency mask) <br>· CMYK (4x8-bit pixels, colour separation) <br>· YCbCr (3x8-bit pixels, colour video format) <br>· I (32-bit signed integer pixels) <br>· F (32-bit floating point pixels)</p>
</blockquote>
<div class="cnblogs_code">
<pre>im = Image.open(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1.png</span><span style="color: rgba(128, 0, 0, 1)">'</span>).convert(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">L</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</pre>
</div>
<p> Filter</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> PIL <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Image, ImageFilter
im </span>= Image.open(‘1<span style="color: rgba(0, 0, 0, 1)">.png’)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 高斯模糊 </span>
<span style="color: rgba(0, 0, 0, 1)">im.filter(ImageFilter.GaussianBlur)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 普通模糊 </span>
<span style="color: rgba(0, 0, 0, 1)">im.filter(ImageFilter.BLUR)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 边缘增强 </span>
<span style="color: rgba(0, 0, 0, 1)">im.filter(ImageFilter.EDGE_ENHANCE)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 找到边缘 </span>
<span style="color: rgba(0, 0, 0, 1)">im.filter(ImageFilter.FIND_EDGES)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 浮雕 </span>
<span style="color: rgba(0, 0, 0, 1)">im.filter(ImageFilter.EMBOSS)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 轮廓 </span>
<span style="color: rgba(0, 0, 0, 1)">im.filter(ImageFilter.CONTOUR)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 锐化 </span>
<span style="color: rgba(0, 0, 0, 1)">im.filter(ImageFilter.SHARPEN)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 平滑 </span>
<span style="color: rgba(0, 0, 0, 1)">im.filter(ImageFilter.SMOOTH)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 细节 </span>
im.filter(ImageFilter.DETAIL)</pre>
</div>
<p> 查看图像直方图</p>
<div class="cnblogs_code">
<pre>im.histogram()</pre>
</div>
<p> 转换图像文件格式</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> img2jpg(imgFile):
</span><span style="color: rgba(0, 0, 255, 1)">if</span> type(imgFile)==str <span style="color: rgba(0, 0, 255, 1)">and</span> imgFile.endswith((<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.bmp</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.gif</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.png</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)):
with Image.open(imgFile) as im:
im.convert(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">RGB</span><span style="color: rgba(128, 0, 0, 1)">'</span>).save(imgFile[:-3]+<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">jpg</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
img2jpg(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1.gif</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
img2jpg(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1.bmp</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
img2jpg(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1.png</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</pre>
</div>
<p> 屏幕截图</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> PIL <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ImageGrab
im </span>= ImageGrab.grab((0,0,800,200)) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">截取屏幕指定区域的图像 </span>
im = ImageGrab.grab() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">不带参数表示全屏幕截图</span></pre>
</div>
<p> 图像裁剪与粘贴</p>
<div class="cnblogs_code">
<pre>box = (120, 194, 220, 294) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">定义裁剪区域 </span>
region = im.crop(box) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">裁剪 </span>
region =<span style="color: rgba(0, 0, 0, 1)"> region.transpose(Image.ROTATE_180)
im.paste(region,box) </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">粘贴</span></pre>
</div>
<p>图像缩放</p>
<div class="cnblogs_code">
<pre>im = im.resize((100,100)) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">参数表示图像的新尺寸,分别表示宽度和高度</span></pre>
</div>
<p>图像对比度增强</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> PIL <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Image
</span><span style="color: rgba(0, 0, 255, 1)">from</span> PIL <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ImageEnhance
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">原始图像</span>
image = Image.open(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">lena.jpg</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
image.show()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">亮度增强</span>
enh_bri =<span style="color: rgba(0, 0, 0, 1)"> ImageEnhance.Brightness(image)
brightness </span>= 1.5<span style="color: rgba(0, 0, 0, 1)">
image_brightened </span>=<span style="color: rgba(0, 0, 0, 1)"> enh_bri.enhance(brightness)
image_brightened.show()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">色度增强</span>
enh_col =<span style="color: rgba(0, 0, 0, 1)"> ImageEnhance.Color(image)
color </span>= 1.5<span style="color: rgba(0, 0, 0, 1)">
image_colored </span>=<span style="color: rgba(0, 0, 0, 1)"> enh_col.enhance(color)
image_colored.show()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">对比度增强</span>
enh_con =<span style="color: rgba(0, 0, 0, 1)"> ImageEnhance.Contrast(image)
contrast </span>= 1.5<span style="color: rgba(0, 0, 0, 1)">
image_contrasted </span>=<span style="color: rgba(0, 0, 0, 1)"> enh_con.enhance(contrast)
image_contrasted.show()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">锐度增强</span>
enh_sha =<span style="color: rgba(0, 0, 0, 1)"> ImageEnhance.Sharpness(image)
sharpness </span>= 3.0<span style="color: rgba(0, 0, 0, 1)">
image_sharped </span>=<span style="color: rgba(0, 0, 0, 1)"> enh_sha.enhance(sharpness)
image_sharped.show()</span></pre>
</div>
<p> </p>
<p><strong>Image模块用法介绍</strong></p>
<p>1. 简介。</p>
<p> 图像处理是一门应用非常广的技术,而拥有非常丰富第三方扩展库的 Python 当然不会错过这一门盛宴。PIL (Python Imaging Library)是 Python 中最常用的图像处理库,目前版本为 1.1.7,我们可以 在这里 下载学习和查找资料。</p>
<p> Image 类是 PIL 库中一个非常重要的类,通过这个类来创建实例可以有直接载入图像文件,读取处理过的图像和通过抓取的方法得到的图像这三种方法。</p>
<p>2. 使用。</p>
<p> 导入 Image 模块。然后通过 Image 类中的 open 方法即可载入一个图像文件。如果载入文件失败,则会引起一个 IOError ;若无返回错误,则 open 函数返回一个 Image 对象。现在,我们可以通过一些对象属性来检查文件内容,即:</p>
<div class="cnblogs_code">
<pre>>>> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Image
</span>>>> im = Image.open(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">j.jpg</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)<br>>>> im.show()
</span>>>> <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)"> im.format, im.size, im.mode
JPEG (</span>440, 330) RGB</pre>
</div>
<p>这里有三个属性,我们逐一了解。</p>
<p> format : 识别图像的源格式,如果该文件不是从文件中读取的,则被置为 None 值。</p>
<p> size : 返回的一个元组,有两个元素,其值为象素意义上的宽和高。</p>
<p> mode : RGB(true color image),此外还有,L(luminance),CMTK(pre-press image)。</p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> PIL <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Image
</span><span style="color: rgba(0, 0, 255, 1)">from</span> PIL <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ImageEnhance
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> pytesseract
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> re
pytesseract.pytesseract.tesseract_cmd </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D:\\Program Files\\Tesseract-OCR\\tesseract.exe</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
tessdata_dir_config </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--tessdata-dir "D:\\Program Files\\Tesseract-OCR\\tessdata"</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
im</span>=Image.open(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">./img/10.jpg</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
im</span>=im.convert(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">L</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
im.show()
im</span>=<span style="color: rgba(0, 0, 0, 1)">ImageEnhance.Contrast(im)
im</span>=im.enhance(1<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">im = im.resize((300, 90))</span>
ltext =<span style="color: rgba(0, 0, 0, 1)"> pytesseract.image_to_string(im)
ltext </span>= re.sub(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\W</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">, ltext)
im.show()
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(ltext)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">print(pytesseract.image_to_string(im))</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">print(pytesseract.image_to_boxes(im))</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">print(im.format, im.size, im.mode)</span></pre>
</div>
<p>convert() : 该函数可以用来将图像转换为不同色彩模式。<br>ImageEnhance.Contrast(im):使用ImageEnhance可以增强图片的识别率</p>
<p>其他</p>
<p>简单的几何变换。</p>
<div class="cnblogs_code">
<pre>>>>out = im.resize((128, 128)) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">调整图片大小</span>
>>>out = im.rotate(45) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">逆时针旋转 45 度角。</span>
>>>out = im.transpose(Image.FLIP_LEFT_RIGHT) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">左右对换。</span>
>>>out = im.transpose(Image.FLIP_TOP_BOTTOM) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">上下对换。</span>
>>>out = im.transpose(Image.ROTATE_90) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">旋转 90 度角。</span>
>>>out = im.transpose(Image.ROTATE_180) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">旋转 180 度角。</span>
>>>out = im.transpose(Image.ROTATE_270) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">旋转 270 度角。</span></pre>
</div>
<p> 序列图像。</p>
<p>即我们常见到的动态图,最常见的后缀为 .gif ,另外还有 FLI / FLC 。PIL 库对这种动画格式图也提供了一些基本的支持。当我们打开这类图像文件时,PIL 自动载入图像的第一帧。我们可以使用 seek 和 tell 方法在各帧之间移动。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Image
im.seek(</span>1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> skip to the second frame</span>
<span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 0, 255, 1)">while</span> 1<span style="color: rgba(0, 0, 0, 1)">:
im.seek( im.tell() </span>+ 1<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> do something to im</span>
<span style="color: rgba(0, 0, 255, 1)">except</span><span style="color: rgba(0, 0, 0, 1)"> EOFError:
</span><span style="color: rgba(0, 0, 255, 1)">pass</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/linyouyi/p/11429511.html
頁:
[1]