深度学习实验:图像处理入门教程(一)- Python Imaging Library(PIL)库使用指南

文章目录

  • 一、实验介绍
  • 二、实验环境
  • 1. 配置虚拟环境
  • 2. 库版本介绍
  • 三、实验内容
  • 0. 安装 PIL 库
  • 1. 图像读取和写入
  • a. 图像读取
  • b. 图像写入
  • c. 构建新图像
  • 2. 图像复制粘贴
  • a. 图像复制
  • b. 图像局部复制
  • c. 图像粘贴
  • 3. 几何变换
  • a. 图像调整大小
  • b. 图像旋转
  • c. 图像翻转
  • 4. 图像增强
  • a. 图像亮度增强
  • b. 图像颜色增强
  • c. 图像对比度增强
  • d. 图像锐度增强
  • 5. 图像滤波
  • a. 图像滤波 – 浮雕
  • b. 图像滤波 – 轮廓
  • 一、实验介绍

      图像处理在深度学习领域中起到了至关重要的作用,Python Imaging Library(PIL)作为一种主流的图像处理库,为图像的读取、处理和增强提供了丰富的功能。

      本实验将介绍 PIL 的基本用法,主要包括图像读取、写入、复制、粘贴、几何变换以及图像增强、图像滤波等方面。

    Pillow v2.4.0 (PIL fork)

    二、实验环境

      本系列实验使用了PyTorch深度学习框架,相关操作如下:

    1. 配置虚拟环境

    conda create -n DL python=3.7 
    
    conda activate DL
    
    pip install torch==1.8.1+cu102 torchvision==0.9.1+cu102 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
    
    conda install matplotlib
    
     conda install scikit-learn
    

    2. 库版本介绍

    软件包 本实验版本 目前最新版
    matplotlib 3.5.3 3.8.0
    numpy 1.21.6 1.26.0
    python 3.7.16
    scikit-learn 0.22.1 1.3.0
    torch 1.8.1+cu102 2.0.1
    torchaudio 0.8.1 2.0.2
    torchvision 0.9.1+cu102 0.15.2

    三、实验内容

    0. 安装 PIL 库

      可以使用以下命令:

    pip install pillow
    

    1. 图像读取和写入

      下面,我将介绍如何使用 PIL 的 Image.open 方法读取图像,并使用 display 方法显示图像。展示了如何使用 save 方法将图像保存到设备上、使用 Image.new 构建新的图像。

    a. 图像读取

    from PIL import Image
    
    # we can use open api to load image data 
    img = Image.open('qomolangma.jpg')
    print(img.format,img.size)
    # show your image
    display(img)
    

    b. 图像写入

    import os
    
    # we can utilize save() to write current image to device.
    file_name = 'qomolangmah.jpg'
    img.save(file_name)
    print(os.path.join(os.getcwd(),file_name))
    

    c. 构建新图像

    image_new = Image.new('RGB', (50, 50), 'red')
    display(image_new)
    file_name = 'new.png'
    image_new.save(file_name)
    print(os.path.join(os.getcwd(), file_name))
    

    2. 图像复制粘贴

      PIL 提供了灵活的图像复制和粘贴功能,下面我将介绍全局级别和局部级别的图像复制,以及使用 crop 方法进行局部图像复制。此外,还有图像的粘贴和合并操作。

    a. 图像复制

    img = Image.open('qomolangma.jpg')
    img_copy = img.copy()
    display(img_copy)
    

    b. 图像局部复制

    rect = (0, 0, 100, 100)
    img_copy_local = img.crop(rect)
    display(img_copy_local)
    

    c. 图像粘贴

    img_new = Image.open('new.png')
    box = (10, 10, 60, 60)
    img.paste(img_new, box)
    display(img)
    

    3. 几何变换

      图像的几何变换是图像处理中的重要任务之一,下面我将详细介绍图像的调整大小、旋转和翻转操作。

    a. 图像调整大小

    img = Image.open('qomolangma.jpg')
    print(img.size)
    img_resize = img.resize((512, 224))
    print(img_resize.size)
    

    b. 图像旋转

    img_rotate = img.rotate(45)
    display(img_rotate)
    


    或使用:

    img_rotate = img.transpose(Image.ROTATE_90)
    img_rotate = img.transpose(Image.ROTATE_180)
    img_rotate = img.transpose(Image.ROTATE_270)
    display(img_rotate)
    

    c. 图像翻转

    img_flip = img.transpose(Image.FLIP_LEFT_RIGHT)
    display(img_flip)
    

    4. 图像增强

      PIL 提供了 ImageEnhanceImageFilter等 模块,用于图像的亮度、颜色、对比度和锐度的增强。下面,我将通过示例演示如何使用这些模块进行图像增强。

    a. 图像亮度增强

    from PIL import ImageEnhance
    
    img = Image.open('qomolangma.jpg')
    img_bri = ImageEnhance.Brightness(img)
    img_bri_enh = img_bri.enhance(factor=0.5) # factor is from 0 to 1. 
    display(img_bri_enh)
    

    b. 图像颜色增强

    img_col = ImageEnhance.Color(img)
    img_col_enh = img_col.enhance(factor=1.5)  # factor is from 0 to postive infinity
    display(img_col_enh)
    

    c. 图像对比度增强

    img_con = ImageEnhance.Contrast(img)
    img_con_enh = img_con.enhance(factor=1.5)  # factor is from 0 to postive infinity
    display(img_con_enh)
    

    d. 图像锐度增强

    img_sha = ImageEnhance.Sharpness(img)
    img_sha_enh = img_sha.enhance(factor=1.5)  # factor is from 0 to 2
    display(img_sha_enh)
    

    5. 图像滤波

      PIL 的 ImageFilter 模块提供了多种滤波操作,如浮雕(EMBOSS)和轮廓(CONTOUR)等。

    a. 图像滤波 – 浮雕

    from PIL.ImageFilter import EMBOSS, CONTOUR
    
    img_1 = img.filter(EMBOSS)
    display(img_1)
    

    b. 图像滤波 – 轮廓

    img_2 = img.filter(CONTOUR)
    display(img_2)
    

    作者:QomolangmaH

    物联沃分享整理
    物联沃-IOTWORD物联网 » 深度学习实验:图像处理入门教程(一)- Python Imaging Library(PIL)库使用指南

    发表回复