• <legend id='PYFGT'><style id='PYFGT'><dir id='PYFGT'><q id='PYFGT'></q></dir></style></legend>

      <small id='PYFGT'></small><noframes id='PYFGT'>

      <tfoot id='PYFGT'></tfoot>

        <bdo id='PYFGT'></bdo><ul id='PYFGT'></ul>
    1. <i id='PYFGT'><tr id='PYFGT'><dt id='PYFGT'><q id='PYFGT'><span id='PYFGT'><b id='PYFGT'><form id='PYFGT'><ins id='PYFGT'></ins><ul id='PYFGT'></ul><sub id='PYFGT'></sub></form><legend id='PYFGT'></legend><bdo id='PYFGT'><pre id='PYFGT'><center id='PYFGT'></center></pre></bdo></b><th id='PYFGT'></th></span></q></dt></tr></i><div id='PYFGT'><tfoot id='PYFGT'></tfoot><dl id='PYFGT'><fieldset id='PYFGT'></fieldset></dl></div>

        Python 图像处理之PIL库详解用法

        Python Imaging Library(PIL)是Python的第三方模块,用于图像处理相关的应用。

              <tbody id='JCa96'></tbody>

              <small id='JCa96'></small><noframes id='JCa96'>

              <legend id='JCa96'><style id='JCa96'><dir id='JCa96'><q id='JCa96'></q></dir></style></legend>
                <bdo id='JCa96'></bdo><ul id='JCa96'></ul>
                <i id='JCa96'><tr id='JCa96'><dt id='JCa96'><q id='JCa96'><span id='JCa96'><b id='JCa96'><form id='JCa96'><ins id='JCa96'></ins><ul id='JCa96'></ul><sub id='JCa96'></sub></form><legend id='JCa96'></legend><bdo id='JCa96'><pre id='JCa96'><center id='JCa96'></center></pre></bdo></b><th id='JCa96'></th></span></q></dt></tr></i><div id='JCa96'><tfoot id='JCa96'></tfoot><dl id='JCa96'><fieldset id='JCa96'></fieldset></dl></div>
                <tfoot id='JCa96'></tfoot>

                  Python 图像处理之PIL库详解用法

                  PIL库简介

                  Python Imaging Library(PIL)是Python的第三方模块,用于图像处理相关的应用。

                  安装PIL库

                  PIL版本更新较慢,建议直接安装Pillow,Pillow是PIL的一个分支项目,它基本保留了PIL的所有功能,并在此基础上提供了一些新的功能以及增强。

                  可以通过pip安装Pillow:

                  pip install Pillow
                  

                  PIL库常用函数

                  打开和保存图像

                  打开图像

                  Image.open(fp, mode='r'):打开图像文件,返回一个图像对象。参数fp指定文件或文件的路径,参数mode可以指定打开模式,默认为'r',只读模式。

                  from PIL import Image
                  
                  image = Image.open('/path/to/image.jpg')
                  

                  保存图像

                  Image.save(fp, format=None, **params):将图像保存到文件或文件流中。参数fp指定文件名和路径,参数format指定文件格式,可以是JPEG、PNG、GIF等格式。另外,还可以使用关键字参数params指定保存参数,比如图片质量等。

                  from PIL import Image
                  
                  image = Image.open('/path/to/image.jpg')
                  image.save('/path/to/save.jpg', format='JPEG', quality=90)
                  

                  图像缩放、裁剪和旋转

                  图像缩放

                  Image.thumbnail(size, resample=3):等比例缩放图像。参数size指定缩放后的大小,resample参数指定重采样算法,默认为3(BICUBIC),建议使用默认值。

                  from PIL import Image
                  
                  image = Image.opne('/path/to/image.jpg')
                  image.thumbnail((300, 300)) # 等比例缩放到300*300像素
                  image.save('/path/to/thumbnail.jpg', format='JPEG', quality=90)
                  

                  图像裁剪

                  Image.crop(box=None):裁剪指定区域的图像,参数box为指定区域的左上角坐标和右下角坐标的元组,格式为(left, up, right, bottom),左上角的坐标为(0, 0)

                  from PIL import Image
                  
                  image = Image.open('/path/to/image.jpg')
                  box = (100, 100, 400, 400) # 左上角坐标为(100, 100),右下角坐标为(400, 400)
                  image_crop = image.crop(box)
                  image_crop.save('/path/to/crop.jpg', format='JPEG', quality=90)
                  

                  图像旋转

                  Image.rotate(angle, resample=3):旋转图像。参数angle为角度值,表示顺时针旋转的角度,可以是正数或负数。resample参数为重采样算法,默认为3(BICUBIC),建议使用默认值。

                  from PIL import Image
                  
                  image = Image.open('/path/to/image.jpg')
                  image_rotate = image.rotate(45) # 顺时针旋转45度
                  image_rotate.save('/path/to/rotate.jpg', format='JPEG', quality=90)
                  

                  示例说明

                  示例1:批量转换图片格式

                  from PIL import Image
                  import os
                  
                  def batch_convert(from_format, to_format, input_dir, output_dir):
                      if not os.path.exists(output_dir):
                          os.mkdir(output_dir)
                  
                      for file_name in os.listdir(input_dir):
                          if file_name.endswith(from_format):
                              file_path = os.path.join(input_dir, file_name)
                              output_path = os.path.join(output_dir, file_name[:-4] + to_format)
                              image = Image.open(file_path)
                              image.save(output_path, format=to_format, quality=90)
                  
                  
                  if __name__ == '__main__':
                      batch_convert('.png', '.jpg', '/path/to/input/dir', '/path/to/output/dir')
                  

                  该示例功能为批量将指定目录下的所有png格式的图片转为jpg格式的图片。

                  示例2:图片水印

                  from PIL import Image, ImageDraw, ImageFont
                  
                  def add_watermark(image_path, text, font_path, font_size):
                      image = Image.open(image_path)
                      draw = ImageDraw.Draw(image)
                      font = ImageFont.truetype(font_path, font_size)
                      text_width, text_height = draw.textsize(text, font)
                      text_x = image.width - text_width
                      text_y = image.height - text_height
                      draw.text((text_x, text_y), text, font=font, fill=(255, 255, 255, 255)) # 在右下角添加白色文本
                      image.save(image_path[:-4] + '_watermark.jpg', format='JPEG', quality=90)
                  
                  
                  if __name__ == '__main__':
                      add_watermark('/path/to/image.jpg', 'test', '/path/to/font.ttf', 30)
                  

                  该示例给指定的图片添加文本水印,使用的字体文件为.ttf格式,字体大小为30。水印文本默认添加在右下角。

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Python中有三个内置函数eval()、exec()和compile()来执行动态代码。这些函数能够从字符串参数中读取Python代码并在运行时执行该代码。但是,使用这些函数时必须小心,因为它们的不当使用可能会导致安全漏洞。
                  在Python中,下载网络文本数据到本地内存是常见的操作之一。本文将介绍四种常见的下载网络文本数据到本地内存的实现方法,并提供示例说明。
                  来给你详细讲解下Python 二进制字节流数据的读取操作(bytes与bitstring)。
                  Python 3.x 是 Python 2.x 的下一个重大版本,其中有一些值得注意的区别。 Python 3.0中包含了许多不兼容的变化,这意味着在迁移到3.0之前,必须进行代码更改和测试。本文将介绍主要的差异,并给出一些实例来说明不同点。
                  要在终端里显示图片,需要使用一些Python库。其中一种流行的库是Pillow,它有一个子库PIL.Image可以加载和处理图像文件。要在终端中显示图像,可以使用如下的步骤:
                  在Python中,我们可以使用Pillow库来进行图像处理。具体实现两幅图像合成一幅图像的方法如下:
                          <tbody id='QCHhm'></tbody>
                        • <bdo id='QCHhm'></bdo><ul id='QCHhm'></ul>

                          <legend id='QCHhm'><style id='QCHhm'><dir id='QCHhm'><q id='QCHhm'></q></dir></style></legend>

                            <tfoot id='QCHhm'></tfoot>
                            <i id='QCHhm'><tr id='QCHhm'><dt id='QCHhm'><q id='QCHhm'><span id='QCHhm'><b id='QCHhm'><form id='QCHhm'><ins id='QCHhm'></ins><ul id='QCHhm'></ul><sub id='QCHhm'></sub></form><legend id='QCHhm'></legend><bdo id='QCHhm'><pre id='QCHhm'><center id='QCHhm'></center></pre></bdo></b><th id='QCHhm'></th></span></q></dt></tr></i><div id='QCHhm'><tfoot id='QCHhm'></tfoot><dl id='QCHhm'><fieldset id='QCHhm'></fieldset></dl></div>

                            <small id='QCHhm'></small><noframes id='QCHhm'>