Python图像处理PIL模块

PIL是Python中用于图像处理的常用库,支持多种格式并提供如裁剪、调整大小和旋转等操作。本文介绍了PIL的基本使用,包括如何导入模块、查看帮助文档,以及重点讲解了open()、crop()和getpixel()函数的功能和示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

图像处理最近几年也是很火的。PIL (Python Imaging Library)是 Python 中最常用的图像处理库,支持众多图像格式,可用于执行裁剪、大小调整、旋转、滤镜效果等操作。

基本说明

导入PIL模块:

import PIL
 from PIL import Image

用的Python2.7上面两个方法都可以。
查看帮助文档:
help(PIL)结果:

 help(PIL)
Help on package PIL:

NAME
    PIL

FILE
    c:\python27\lib\site-packages\pil\__init__.py

DESCRIPTION
    # The Python Imaging Library.
    # $Id$
    #
    # package placeholder
    #
    # Copyright (c) 1999 by Secret Labs AB.
    #
    # See the README file for information on usage and redistribution.
    #

PACKAGE CONTENTS
    ArgImagePlugin
    BdfFontFile
    BmpImagePlugin
    BufrStubImagePlugin
    ContainerIO
    CurImagePlugin
    DcxImagePlugin
    EpsImagePlugin
    ExifTags
    FitsStubImagePlugin
    FliImagePlugin
    FontFile
    FpxImagePlugin
    GbrImagePlugin
    GdImageFile
    GifImagePlugin
    GimpGradientFile
    GimpPaletteFile
    GribStubImagePlugin
    Hdf5StubImagePlugin
    IcnsImagePlugin
    IcoImagePlugin
    ImImagePlugin
    Image
    ImageChops
    ImageCms
    ImageColor
    ImageDraw
    ImageDraw2
    ImageEnhance
    ImageFile
    ImageFileIO
    ImageFilter
    ImageFont
    ImageGrab
    ImageMath
    ImageMode
    ImageOps
    ImagePalette
    ImagePath
    ImageQt
    ImageSequence
    ImageShow
    ImageStat
    ImageTk
    ImageTransform
    ImageWin
    ImtImagePlugin
    IptcImagePlugin
    Jpeg2KImagePlugin
    JpegImagePlugin
    JpegPresets
    McIdasImagePlugin
    MicImagePlugin
    MpegImagePlugin
    MspImagePlugin
    OleFileIO
    PSDraw
    PaletteFile
    PalmImagePlugin
    PcdImagePlugin
    PcfFontFile
    PcxImagePlugin
    PdfImagePlugin
    PixarImagePlugin
    PngImagePlugin
    PpmImagePlugin
    PsdImagePlugin
    PyAccess
    SgiImagePlugin
    SpiderImagePlugin
    SunImagePlugin
    TarIO
    TgaImagePlugin
    TiffImagePlugin
    TiffTags
    WalImageFile
    WebPImagePlugin
    WmfImagePlugin
    XVThumbImagePlugin
    XbmImagePlugin
    XpmImagePlugin
    _binary
    _imaging
    _imagingcms
    _imagingft
    _imagingmath
    _imagingtk
    _util
    _webp
    tests

DATA
    PILLOW_VERSION = '2.4.0'
    VERSION = '1.1.7'

help(Image)结果:

help(Image)
Help on module PIL.Image in PIL:

NAME
    PIL.Image

FILE
    c:\python27\lib\site-packages\pil\image.py

DESCRIPTION
    # The Python Imaging Library.
    # $Id$
    #
    # the Image class wrapper
    #
    # partial release history:
    # 1995-09-09 fl   Created
    # 1996-03-11 fl   PIL release 0.0 (proof of concept)
    # 1996-04-30 fl   PIL release 0.1b1
    # 1999-07-28 fl   PIL release 1.0 final
    # 2000-06-07 fl   PIL release 1.1
    # 2000-10-20 fl   PIL release 1.1.1
    # 2001-05-07 fl   PIL release 1.1.2
    # 2002-03-15 fl   PIL release 1.1.3
    # 2003-05-10 fl   PIL release 1.1.4
    # 2005-03-28 fl   PIL release 1.1.5
    # 2006-12-02 fl   PIL release 1.1.6
    # 2009-11-15 fl   PIL release 1.1.7
    #
    # Copyright (c) 1997-2009 by Secret Labs AB.  All rights reserved.
    # Copyright (c) 1995-2009 by Fredrik Lundh.
    #
    # See the README file for information on usage and redistribution.
    #

CLASSES
    Image
    ImagePointHandler
    ImageTransformHandler

    class Image
     |  This class represents an image object.  To create
     |  :py:class:`~PIL.Image.Image` objects, use the appropriate factory
     |  functions.  There's hardly ever any reason to call the Image constructor
     |  directly.
     |  
     |  * :py:func:`~PIL.Image.open`
     |  * :py:func:`~PIL.Image.new`
     |  * :py:func:`~PIL.Image.frombytes`
     |  
     |  Methods defined here:
     |  
     |  __getattr__(self, name)
     |  
     |  __init__(self)
     |  
     |  __repr__(self)
     |  
     |  convert(self, mode=None, matrix=None, dither=None, palette=0, colors=256)
     |      Returns a converted copy of this image. For the "P" mode, this
     |      method translates pixels through the palette.  If mode is
     |      omitted, a mode is chosen so that all information in the image
     |      and the palette can be represented without a palette.
     |      
     |      The current version supports all possible conversions between
     |      "L", "RGB" and "CMYK." The **matrix** argument only supports "L"
     |      and "RGB".
     |      
     |      When translating a color image to black and white (mode "L"),
     |      the library uses the ITU-R 601-2 luma transform::
     |      
     |          L = R * 299/1000 + G * 587/1000 + B * 114/1000
     |      
     |      The default method of converting a greyscale ("L") or "RGB"
     |      image into a bilevel (mode "1") image uses Floyd-Steinberg
     |      dither to approximate the original image luminosity levels. If
     |      dither is NONE, all non-zero values are set to 255 (white). To
     |      use other thresholds, use the :py:meth:`~PIL.Image.Image.point`
     |      method.
     |      
     |      :param mode: The requested mode.
     |      :param matrix: An optional conversion matrix.  If given, this
     |         should be 4- or 16-tuple containing floating point values.
     |      :param dither: Dithering method, used when converting from
     |         mode "RGB" to "P" or from "RGB" or "L" to "1".
     |         Available methods are NONE or FLOYDSTEINBERG (default).
     |      :param palette: Palette to use when converting from mode "RGB"
     |         to "P".  Available palettes are WEB or ADAPTIVE.
     |      :param colors: Number of colors to use for the ADAPTIVE palette.
     |         Defaults to 256.
     |      :rtype: :py:class:`~PIL.Image.Image`
     |      :returns: An :py:class:`~PIL.Image.Image` object.
     |  
     |  copy(self)
     |      Copies this image. Use this method if you wish to paste things
     |      into an image, but still retain the original.
     |      
     |      :rtype: :py:class:`~PIL.Image.Image`
     |      :returns: An :py:class:`~PIL.Image.Image` object.
     |  
     |  crop(self, box=None)
     |      Returns a rectangular region from this image. The box is a
     |      4-tuple defining the left, upper, right, and lower pixel
     |      coordinate.
     |      
     |      This is a lazy operation.  Changes to the source image may or
     |      may not be reflected in the cropped image.  To break the
     |      connection, call the :py:meth:`~PIL.Image.Image.load` method on
     |      the cropped copy.
     |      
     |      :param box: The crop rectangle, as a (left, upper, right, lower)-tuple.
     |      :rtype: :py:class:`~PIL.Image.Image`
     |      :returns: An :py:class:`~PIL.Image.Image` object.
     |  
     |  draft(self, mode, size)
     |      NYI
     |      
     |      Configures the image file loader so it returns a version of the
     |      image that as closely as possible matches the given mode and
     |      size.  For example, you can use this method to convert a color
     |      JPEG to greyscale while loading it, or to extract a 128x192
     |      version from a PCD file.
     |      
     |      Note that this method modifies the :py:class:`~PIL.Image.Image` object
     |      in place.  If the image has already been loaded, this method has no
     |      effect.
     |      
     |      :param mode: The requested mode.
     |      :param size: The requested size.
     |  
     |  filter(self, filter)
     |      Filters this image using the given filter.  For a list of
     |      available filters, see the :py:mod:`~PIL.ImageFilter` module.
     |      
     |      :param filter: Filter kernel.
     |      :returns: An :py:class:`~PIL.Image.Image` object.
     |  
     |  frombytes(self, data, decoder_name='raw', *args)
     |      Loads this image with pixel data from a bytes object.
     |      
     |      This method is similar to the :py:func:`~PIL.Image.frombytes` function,
     |      but loads data into this image instead of creating a new image object.
     |  
     |  fromstring(self, *args, **kw)
     |      Deprecated alias to frombytes.
     |      
     |      .. deprecated:: 2.0
     |  
     |  getbands(self)
     |      Returns a tuple containing the name of each band in this image.
     |      For example, **getbands** on an RGB image returns ("R", "G", "B").
     |      
     |      :returns: A tuple containing band names.
     |      :rtype: tuple
     |  
     |  getbbox(self)
     |      Calculates the bounding box of the non-zero regions in the
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值