PIL包里面的Image模块里面的函数讲解

 导入该模块

from PIL import Image # 导入Image模块

它内置的属性如下:

class Image(object):
    def __init__(self):
        # FIXME: take "new" parameters / other image?
        # FIXME: turn mode and size into delegating properties?
        self.im = None
        self.mode = ""
        self.size = (0, 0)
        self.palette = None
        self.info = {}
        self.category = NORMAL
        self.readonly = 0
        self.pyaccess = None

    @property
    def width(self):
        return self.size[0]

    @property
    def height(self):
        return self.size[1]

因此

_llg = Image.open('/home/zzp/Test/g31.png') # 这是一张(1024,2048,4)的0-255的图片
_llg.size  # (2048,1024)
_llg.im    #  <ImagingCore at 0x7f3a382e52f0>
_llg.mode  # 'RGBA'
_llg.info  # {}
_llg.category  # 0
_llg.readonly  # 0
_llg.pyaccess  # 无输出
_llg.width     # 2048
_llg.height    # 1024
type(_llg)     #  PIL.PngImagePlugin.PngImageFile

_img = Image.open('/home/zzp/Test/t3.png') # 这是一张(1024,2048,3)的0-255的图片
_img.mode  # 'RGB'
_img.info  # {'aspect': (72, 72), 'gamma': 0.45455}
# 其他的参数和上面相同

_lg = Image.open('/home/zzp/Test/g34.png')  # 这是一张(1024,2048)的0-255的图片
_lg.mode  # 'L'
_lg.info  # {}
# 其他参数和上面一样

它内置的方法如下:

def tobytes(self, encoder_name="raw", *args):  # 下面的方法都只写它的名字和调用参数了
    pass #  Return image as a bytes object.
def tobitmap(self, name="image"):
    pass # Returns the image converted to an X11 bitmap.
def frombytes(self, data, decoder_name="raw", *args):
    pass  # Loads this image with pixel data from a bytes object.
def load(self):
    pass  # Allocates storage for the image and loads the pixel data.  In
          # normal cases, you don't need to call this method, since the
          # Image class automatically loads an opened image when it is
          # accessed for the first time. This method will close the file
          # associated with the image.
def verify(self):
    '''
        Verifies the contents of a file. For data read from a file, this
        method attempts to determine if the file is broken, without
        actually decoding the image data.  If this method finds any
        problems, it raises suitable exceptions.  If you need to load
        the image after using this method, you must reopen the image
        file.'''
    pass
def copy(self):
    pass  # Copies this image. Use this method if you wish to paste things into an image, but still retain the original.
def crop(self, box=None):
    pass  # Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.

下面的方法比较常用

def resize(self, size, resample=NEAREST):
    pass  # Returns a resized copy of this image.
    ''' size: The requested size in pixels, as a 2-tuple:(width, height).
        resample: An optional resampling filter.  This can be one of :py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BOX`, :py:attr:`PIL.Image.BILINEAR`, :py:attr:`PIL.Image.HAMMING`, :py:attr:`PIL.Image.BICUBIC` or :py:attr:`PIL.Image.LANCZOS`. If omitted(省略), or if the image has mode "1" or "P", it is set :py:attr:`PIL.Image.NEAREST`.
    '''
def rotate(self, angle, resample=NEAREST, expand=0, center=None, translate=None):
    pass # Returns a rotated copy of this image.  This method returns a copy of this image, rotated the given number of degrees counter clockwise around its centre.
    ''' angle: 逆时针方向,是一个number,不要求是整数
        resample:同上
        expand: If True,expands the output image to make it large enough to hold the entire rotated image. If False or omitted,make the output image the same size as the input image.
        center: center of rotation (a 2-tuple)旋转中心,默认是图片的中点。
    '''
def save(self, filename, format=None, **params):
    pass  # 保存图片在filename路径下,如果format默认则该图片保存的类型是filename写明的文件类型,如果filename只有路径没有文件名字,那么format一定要指定文件格式
def show(self, title=None, command=None):
    pass  # Displays this image. This method is mainly intended for debugging purposes.
def transpose(self, method):
    pass  # Transpose image (flip or rotate in 90 degree steps)
# method = PIL.Image.FLIP_LEFT_RIGHT左右颠倒,PIL.Image.FLIP_TOP_BOTTOM上下颠倒,PIL.Image.ROTATE_90/180/270顺时针旋转90度,以及180度,270度,PIL.Image.TRANSPOSE不明
def convert(self, mode=None, matrix=None, dither=None,palette=WEB, colors=256):
    pass  # 转换mode

还可以参考http://hereson.iteye.com/blog/2224334

猜你喜欢

转载自blog.csdn.net/zz2230633069/article/details/84667236
今日推荐