苹果mac中安装python|苹果MAC中安装Python图像处理库PIL

更新时间:2019-09-03    来源:图像处理    手机版     字体:

【www.bbyears.com--图像处理】

开发平台是Mac,需要用到Python的图像处理库PIL,下面记录了安装过程以及出现的问题。
基本安装过程是这样的,使用命令pip进行安装

 代码如下

$ pip install PIL
Downloading/unpacking PIL
  Could not find any downloads that satisfy the requirement PIL
  Some externally hosted files were ignored (use --allow-external PIL to allow).
Cleaning up...
No distributions at all found for PIL
Storing debug log for failure in /Users/mylxsw/.pip/pip.log

提示需要添加--allow-external参数

 代码如下

$ pip2.7 install PIL --allow-external PIL
Downloading/unpacking PIL
  Could not find any downloads that satisfy the requirement PIL
  Some insecure and unverifiable files were ignored (use --allow-unverified PIL to allow).
Cleaning up...
No distributions at all found for PIL
Storing debug log for failure in /Users/mylxsw/.pip/pip.log

又报错了,提示需要添加--allow-unverified参数

$ pip2.7 install PIL --allow-external PIL --allow-unverified PIL
...
_imagingft.c:73:10: fatal error: "freetype/fterrors.h" file not found

#include

         ^
1 error generated.
...

提示缺少freetype/fterrors.h头文件,可是系统已经安装了,于是从stackoverflow上找到方案:

 代码如下

$ ln -s /usr/local/include/freetype2 /usr/local/include/freetype

再次安装

 代码如下

$ pip2.7 install PIL --allow-external PIL --allow-unverified PIL
Downloading/unpacking PIL
...
Successfully installed PIL
Cleaning up...

下面看一些例子

  导入 Image 模块。然后通过 Image 类中的 open 方法即可载入一个图像文件。如果载入文件失败,则会引起一个 IOError ;若无返回错误,则 open 函数返回一个 Image 对象。现在,我们可以通过一些对象属性来检查文件内容,即:

 代码如下

 >>> import Image
  >>> im = Image.open("j.jpg")
  >>> print im.format, im.size, im.mode

4 JPEG (440, 330) RGB

    这里有三个属性,我们逐一了解。

        format : 识别图像的源格式,如果该文件不是从文件中读取的,则被置为 None 值。

        size : 返回的一个元组,有两个元素,其值为象素意义上的宽和高。

        mode : RGB(true color image),此外还有,L(luminance),CMTK(pre-press image)。

    现在,我们可以使用一些在 Image 类中定义的方法来操作已读取的图像实例。比如,显示最新载入的图像:

1 >>>im.show()
2  >>>
    输出原图:

苹果MAC中安装Python图像处理库PIL

3. 函数概貌。

3.1    Reading and Writing Images : open( infilename ) , save( outfilename )

3.2    Cutting and Pasting and Merging Images :

        crop() : 从图像中提取出某个矩形大小的图像。它接收一个四元素的元组作为参数,各元素为(left, upper, right, lower),坐标系统的原点(0, 0)是左上角。

        paste() :

        merge() :

 代码如下

>>> box = (100, 100, 200, 200)
 >>> region = im.crop(box)
 >>> region.show()
 >>> region = region.transpose(Image.ROTATE_180)
 >>> region.show()
 >>> im.paste(region, box)
 >>> im.show()

   其效果图为:

苹果MAC中安装Python图像处理库PIL

    旋转一幅图片:

 代码如下

def roll(image, delta):
    "Roll an image sideways"

    xsize, ysize = image.size

    delta = delta % xsize
    if delta == 0: return image

    part1 = image.crop((0, 0, delta, ysize))
    part2 = image.crop((delta, 0, xsize, ysize))
    image.paste(part2, (0, 0, xsize-delta, ysize))
    image.paste(part1, (xsize-delta, 0, xsize, ysize))

    return image

 
3.3    几何变换。
3.3.1    简单的几何变换。

 代码如下

>>>out = im.resize((128, 128))                     #
 >>>out = im.rotate(45)                             #逆时针旋转 45 度角。
 >>>out = im.transpose(Image.FLIP_LEFT_RIGHT)       #左右对换。
 >>>out = im.transpose(Image.FLIP_TOP_BOTTOM)       #上下对换。
 >>>out = im.transpose(Image.ROTATE_90)             #旋转 90 度角。
 >>>out = im.transpose(Image.ROTATE_180)            #旋转 180 度角。
>>>out = im.transpose(Image.ROTATE_270)            #旋转 270 度角。


    各个调整之后的图像为:

 

 各个调整之后的图像为:     图片1:苹果MAC中安装Python图像处理库PIL     图片2:苹果MAC中安装Python图像处理库PIL     图片3:苹果MAC中安装Python图像处理库PIL     图片4:苹果MAC中安装Python图像处理库PIL

3.3.2    色彩空间变换。

    convert() : 该函数可以用来将图像转换为不同色彩模式。

3.3.3    图像增强。

    Filters : 在 ImageFilter 模块中可以使用 filter 函数来使用模块中一系列预定义的增强滤镜。

 代码如下

 >>> import ImageFilter
 >>> imfilter = im.filter(ImageFilter.DETAIL)
 >>> imfilter.show()

3.4    序列图像。

    即我们常见到的动态图,最常见的后缀为 .gif ,另外还有 FLI / FLC 。PIL 库对这种动画格式图也提供了一些基本的支持。当我们打开这类图像文件时,PIL 自动载入图像的第一帧。我们可以使用 seek 和 tell 方法在各帧之间移动。

 代码如下

import Image
im.seek(1)        # skip to the second frame

try:
    while 1:
        im.seek( im.tell() + 1)
        # do something to im
except EOFError:
    pass

 
3.5    更多关于图像文件的读取。

    最基本的方式:im = Image.open("filename")

    类文件读取:fp = open("filename", "rb"); im = Image.open(fp)

    字符串数据读取:import StringIO; im = Image.open(StringIO.StringIO(buffer))

    从归档文件读取:import TarIO; fp = TarIo.TarIO("Image.tar", "Image/test/lena.ppm"); im = Image.open(fp)

本文来源:http://www.bbyears.com/jiaocheng/65988.html